🌱BeginnerPython Fundamentals
Strings
Creating, formatting, and manipulating text with Python's built-in string methods.
Overview
Strings in Python are sequences of characters enclosed in single quotes ('hello'), double quotes ("hello"), or triple quotes ("""multi-line text"""). Unlike some languages, Python treats single and double quotes identically for regular strings choose whichever avoids the need to escape the other.
Strings are immutable once created, you cannot change individual characters in place. Operations that appear to modify a string actually return a new string.
Python's string type comes with a rich set of built-in methods (upper(), lower(), strip(), split(), replace(), and many more) and supports powerful formatting via f-strings (formatted string literals, introduced in Python 3.6). f-strings let you embed expressions directly inside string literals using {expression} syntax, making them the preferred way to build strings from variables.
Key Points
- •Single and double quotes are interchangeable: "hello" and 'hello' are the same string
- •Triple quotes (""" or ''') allow multi-line strings and embedded quotes without escaping
- •Strings are immutable methods return new strings, they never modify the original
- •f-strings: f"Hello, {name}!" embeds expressions inline the preferred modern formatting style
- •len(s) returns the number of characters; s[0] accesses the first character; s[-1] the last
- •Slicing: s[start:stop:step] extracts a substring (stop is exclusive)
- •The in operator checks for substrings: "ell" in "hello" → True
Examples
String creation and basic operations
greeting = "Hello, World!"
print(len(greeting)) # 13
print(greeting[0]) # H
print(greeting[-1]) # !
print(greeting[7:12]) # World
print(greeting.lower()) # hello, world!
print(greeting.upper()) # HELLO, WORLD!f-strings the modern way to format strings
name = "Alice"
age = 30
height = 1.75
print(f"My name is {name} and I am {age} years old.")
print(f"Height: {height:.2f} meters") # format to 2 decimal places
print(f"Next year I will be {age + 1}") # expressions work inside {}f-strings (Python 3.6+) are preferred over older .format() and % formatting.
Useful string methods
s = " hello, world! "
print(s.strip()) # "hello, world!" (removes leading/trailing whitespace)
print(s.strip().title()) # "Hello, World!"
print(s.strip().replace("world", "Python")) # "hello, Python!"
csv = "apple,banana,cherry"
parts = csv.split(",")
print(parts) # ['apple', 'banana', 'cherry']
print(", ".join(parts)) # "apple, banana, cherry"Common Mistakes
- Trying to modify a character in place: s[0] = "H" raises TypeError because strings are immutable
- Forgetting that string indices start at 0, not 1
- Using + to concatenate many strings in a loop (slow) prefer join() or f-strings for efficiency
Note
To check all methods available on a string, type `dir("")` in the Python REPL. To read the docs for any method, use `help("".strip)` right in the REPL.
Exercises
Write Python code to solve each exercise. Run it in your browser and check your output.
Loading editor…
Completed: 0 / 5 exercises
Knowledge Check
Question 1 of 4