String
A string in Python is an immutable sequence of Unicode characters. You can create strings with single quotes ('hello'), double quotes ("hello"), or triple ...
String Basics
A string in Python is an immutable sequence of Unicode characters. You can create strings with single quotes ('hello'), double quotes ("hello"), or triple quotes ("""multi-line"""). Triple-quoted strings preserve newlines and are used for docstrings. Strings are immutable — you cannot change individual characters in place. s = "hello"; s[0] = "H" raises TypeError. Instead, you create a new string: s = "H" + s[1:]. Python strings are stored as Unicode by default in Python 3.
String Indexing and Slicing
Individual characters are accessed by index starting at 0. s = "Python"; s[0] is "P"; s[-1] is "n" (negative indices count from the end). Slicing extracts a substring: s[1:4] is "yth"; s[:3] is "Pyt"; s[3:] is "hon"; s[-3:] is "hon". The full slice syntax is s[start:stop:step]. s[::-1] reverses the string. Slicing never raises IndexError — if the slice bounds exceed the string length, Python silently clips them.
Common String Methods
Python strings have many built-in methods. upper() and lower() change case. strip(), lstrip(), rstrip() remove whitespace (or specified characters) from the ends. split(sep) splits into a list; join(iterable) joins a list into a string: ",".join(["a","b","c"]) gives "a,b,c". replace(old, new) replaces occurrences. find(sub) returns the index of the first occurrence or -1. startswith(prefix) and endswith(suffix) return booleans. count(sub) counts non-overlapping occurrences.
f-strings and String Formatting
f-strings (Python 3.6+) are the preferred way to embed values in strings: name = "Alice"; f"Hello, {name}!" gives "Hello, Alice!". Format specifiers inside curly braces control number formatting: f"{3.14159:.2f}" gives "3.14"; f"{1000000:,}" gives "1,000,000". Older approaches include str.format(): "Hello, {}!".format("Alice") and %-formatting: "Hello, %s!" % "Alice". f-strings are fastest and most readable; prefer them in new code.
String Immutability and Performance
Because strings are immutable, concatenating inside a loop creates a new string object on every iteration: result = ""; for s in items: result += s creates O(n^2) work. The efficient pattern is to collect strings in a list and join at the end: result = "".join(items). The join approach is O(n) because Python can pre-calculate the required memory. This optimisation is critically important when building large strings from many pieces.
Exam Tip
Common string exam traps: "abc"[1:10] does not raise IndexError — it gives "bc". "abc" * 0 gives "". "abc" == "ABC" is False (case-sensitive). The in operator checks for substrings: "bc" in "abcd" is True. A classic question: what does "hello".split() return when called with no arguments? It splits on any whitespace and returns ["hello"]. What does " a b c ".split() return? ["a", "b", "c"] — leading/trailing whitespace is ignored when no separator is specified.