Basics#
Strings are immutable sequences of characters, written in quotes. Indexing: forward from 0, backward from −1. Slicing s[start:stop:step].
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 |
स्ट्रिंग अपरिवर्तनशील (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.