Skip to content
  • Strings · Python

Strings

LearnViaNotes @learnvianotes· 2 min read

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.

Python
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 itriw

Immutability

Python
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)                         # bat

Essential methods

MethodDoesExample → 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 / endswithPrefix / suffix test'file.py'.endswith('.py') → True
isdigit / isalpha / isalnumCharacter classes'123'.isdigit() → True
count(x)Non-overlapping occurrences'banana'.count('a') → 3
Python
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 Fox

f-strings & formatting

Python
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,000

Characters, Unicode & bytes

Python
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

Python
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

Free for students and teachers

Write notes like this one

Cards for every idea, quizzes and flashcards, read aloud — on the web and your phone.

Something wrong with this note? Open it to report