A string is an immutable sequence of characters. In Python, strings support indexing, slicing, and a rich set of built-in methods.
| Operation | Time |
|---|---|
| Index / Slice | O(1) / O(k) |
| Search (in, find) | O(n) |
| Concatenation | O(n + m) |
| Split / Join | O(n) |
# Slicing
text = "PythonProgramming"
text[:6] # "Python"
text[-11:] # "Programming"
# Case
"hello".upper(); "HELLO".lower()
# Find & Replace
"hello".find("ll") # 2
"hello".replace("ll", "rr") # "herro"
# Split & Join
"a,b,c".split(",") # ['a', 'b', 'c']
"-".join(['a', 'b', 'c']) # "a-b-c"
# Substring checks
"Python" in "Hello, Python!" # True
"banana".count("an") # 2
# Formatting
f"My name is {name} and I am {age} years old."