str is an immutable sequence of Unicode characters — indexing, slicing and methods all return new strings.
Indexing & slicing
s[start:stop:step] — start is included, stop is excluded, negative indices count from the end.
s = "interview"
print(s[0], s[-1], len(s)) # i w 9
print(s[0:5], s[5:], s[-4:]) # inter view view
print(s[::-1], s[::2]) # weivretni itriwImmutability
s = "cat"
try:
s[0] = "b"
except TypeError as e:
print("TypeError:", e) # TypeError: 'str' object does not support item assignment
s = "b" + s[1:] # build a new string instead
print(s) # batEssential methods
| Method | Does | Example → result |
|---|---|---|
split(sep) / join(it) | String ↔ list | 'a,b'.split(',') → ['a', 'b'] |
strip() / lstrip() / rstrip() | Trim whitespace (or given chars) | ' hi '.strip() → 'hi' |
lower() / upper() / title() | Change case | 'Py'.lower() → 'py' |
find(x) / index(x) | Position or −1 / raises | 'abc'.find('z') → -1 |
replace(a, b) | Replace all occurrences | 'aa'.replace('a','b') → 'bb' |
startswith / endswith | Prefix / suffix test | 'file.py'.endswith('.py') → True |
isdigit / isalpha / isalnum | Character classes | '123'.isdigit() → True |
count(x) | Non-overlapping occurrences | 'banana'.count('a') → 3 |
words = " the quick brown fox ".split() # split() with no args handles runs of spaces
print(words) # ['the', 'quick', 'brown', 'fox']
print("-".join(words)) # the-quick-brown-fox
print(" ".join(w.capitalize() for w in words)) # The Quick Brown Foxf-strings & formatting
name, score, ratio = "Asha", 93, 0.8567
print(f"{name} scored {score}") # Asha scored 93
print(f"{ratio:.2%} | {score:05d}") # 85.67% | 00093
print(f"{name!r:>8}|{score:<5}|") # 'Asha'|93 |
print(f"{score=}") # score=93
print(f"{1_000_000:,}") # 1,000,000Characters, Unicode & bytes
print(ord("A"), chr(97)) # 65 a
print(len("naïve"), len("naïve".encode("utf-8"))) # 5 6
print("é".encode(), b"\xc3\xa9".decode()) # b'\xc3\xa9' éInterview favourites
def is_palindrome(s: str) -> bool:
cleaned = [c.lower() for c in s if c.isalnum()]
return cleaned == cleaned[::-1]
def is_anagram(a: str, b: str) -> bool:
from collections import Counter
return Counter(a) == Counter(b)
print(is_palindrome("A man, a plan, a canal: Panama")) # True
print(is_anagram("listen", "silent")) # True