Strings and f-Strings
Strings are how Python stores text: names, messages, file paths, anything made of characters. In this lesson you will create strings, pull pieces out of them, and format them with f-strings, the modern way to build text.
What You'll Learn
- Creating strings and joining them
- Indexing and slicing to grab characters or parts
- Formatting values cleanly with f-strings
Creating Strings
Use single or double quotes. Triple quotes hold multi-line text:
Joining and Repeating
+ glues strings together; * repeats them:
Indexing
Each character has a position, starting at 0. Negative numbers count from the end:
Slicing
Grab a range with [start:end]. The start is included, the end is not:
f-Strings: the Best Way to Format
You met f-strings in the Mad-Libs project. They put values (and even expressions) directly inside text:
f-strings can format numbers too. This is how you show clean decimals, percentages, and thousands separators:
Strings Are Immutable
You cannot change a character in place, but you can build a new string:
Key Takeaways
- Strings use single, double, or triple quotes
- Index from
0, or from-1at the end; slice with[start:end] [::-1]reverses a string- f-strings insert values:
f'Hi, {name}' - Format numbers inside f-strings:
:.2f,:,,:.1%

