
Strings are sequences of characters. Immutable in Python.
Creating strings:
s1 = 'single quotes' s2 = "double quotes" s3 = """triple quotes for multi-line"""
Essential string methods:
s = "Hello World"
s.upper() # "HELLO WORLD"
s.lower() # "hello world"
s.strip() # removes whitespace
s.replace("World", "Python") # "Hello Python"
s.split(" ") # ["Hello", "World"]
len(s) # 11f-strings (use these):
name = "Alice"
age = 30
print(f"My name is {name} and I am {age} years old.")Indexing & slicing:
s = "Python" s[0] # "P" s[-1] # "n" s[1:4] # "yth" s[::-1] # "nohtyP" (reversed)
Reference:
TaskLoco™ — The Sticky Note GOAT