⚡0 XP🔥0

Ch 7 · Strings | स्ट्रिंग

Lesson 7 of 11 ⏱ 3 min read ✎ 1 exercises ❓ 5-question quiz ▶ Runs Python

Strings as sequences, indexing and slicing, operations (concatenation, repetition, membership), traversal, built-in string methods and programs.

Basics#

Strings are immutable sequences of characters, written in quotes. Indexing: forward from 0, backward from −1. Slicing s[start:stop:step].

▶ Try it YourselfPython
Ctrl + Enter to run

Common methods#

Method Does
upper(), lower(), title(), capitalize() change case
find(sub), index(sub), count(sub) search
replace(old, new) replace
split(), join() break into list / join list
strip(), lstrip(), rstrip() remove spaces
isalpha(), isdigit(), isalnum(), isspace(), isupper(), islower() tests
startswith(), endswith() check prefix/suffix
▶ Try it YourselfPython
Ctrl + Enter to run
हिंदी में समझें

स्ट्रिंग अपरिवर्तनशील (immutable) होती है - उसके किसी अक्षर को सीधे नहीं बदल सकते। s[::-1] से स्ट्रिंग उलट जाती है। split() स्ट्रिंग को शब्दों की सूची में बदलता है और join() सूची को स्ट्रिंग में।

Programs#

Count vowels, check palindrome, count words, convert case, find the frequency of a character.

Exam tips#

  • find() returns −1 if not found; index() raises ValueError.

Practice Exercises

Write your code, then press Check. Your program is tested automatically.

1

Palindrome check

Read a word and print Palindrome if it reads the same backwards (ignore case), else Not Palindrome.

Test input:
Level
Expected output:
Palindrome

Quick Quiz

Q1.

'Python'[1:4] is

Q2.

Strings in Python are

Q3.

'abc'.upper() gives

Q4.

find() returns ___ when not found.

Q5.

'a-b-c'.split('-') gives