Concatenation, Repetition, and Basic Formatting
You often need to build messages from pieces (names, numbers, results). Python gives you three core tools: +
to glue a few strings, *
to repeat a string, and f-strings to format values neatly. When joining many pieces (e.g., a list of words), use str.join
.
Concatenation with +
Use +
to join a small number of strings. If you need to combine text with numbers, convert the number or use an f-string.
1234567first = "Hello" second = "world" msg = first + " " + second # "Hello world" score = 10 # "score: " + score # TypeError msg2 = "score: " + str(score) # OK: "score: 10"
Repetition with *
Multiply a string by an integer to repeat it.
12echo = "ha" * 3 # "hahaha" rule = "-" * 10 # "----------"
Joining many pieces with "sep".join(...)
join
is ideal when you have an iterable (like a list) of strings.
12345words = ["Python", "is", "fun"] sentence = " ".join(words) # "Python is fun" lines = ["ID: 42", "OK", "Done"] block = "\n".join(lines) # "ID: 42\nOK\nDone"
Basic formatting with f-strings
An f-string evaluates expressions inside {}
and inserts the result. It’s concise and handles type conversion automatically.
123name = "Ada" tasks = 3 print(f"{name} completed {tasks} tasks.") # "Ada completed 3 tasks."
Numeric formatting (common cases).
1234price = 12.5 tax = 0.2 total = price * (1 + tax) print(f"Total: {total:.2f}") # 2 decimal places, e.g., "Total: 15.00"
If you need a literal brace, double it.
1print(f"Use {{}} for placeholders.") # "Use {} for placeholders."
For long loops that build big strings, collect pieces and then ''.join(pieces)
(you saw why in the immutability chapter).
1. You have items = ["red", "green", "blue"]
. Which is the best way to produce "red, green, blue"
?
2. What does "ha" * 2 + "!"
produce?
3. Which line prints a price with two decimal places if total = 7.5
?
Tak for dine kommentarer!
Spørg AI
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat
Can you explain when to use f-strings versus str.join?
What happens if I try to join non-string elements with join?
Can you show more examples of numeric formatting with f-strings?
Awesome!
Completion rate improved to 5
Concatenation, Repetition, and Basic Formatting
Stryg for at vise menuen
You often need to build messages from pieces (names, numbers, results). Python gives you three core tools: +
to glue a few strings, *
to repeat a string, and f-strings to format values neatly. When joining many pieces (e.g., a list of words), use str.join
.
Concatenation with +
Use +
to join a small number of strings. If you need to combine text with numbers, convert the number or use an f-string.
1234567first = "Hello" second = "world" msg = first + " " + second # "Hello world" score = 10 # "score: " + score # TypeError msg2 = "score: " + str(score) # OK: "score: 10"
Repetition with *
Multiply a string by an integer to repeat it.
12echo = "ha" * 3 # "hahaha" rule = "-" * 10 # "----------"
Joining many pieces with "sep".join(...)
join
is ideal when you have an iterable (like a list) of strings.
12345words = ["Python", "is", "fun"] sentence = " ".join(words) # "Python is fun" lines = ["ID: 42", "OK", "Done"] block = "\n".join(lines) # "ID: 42\nOK\nDone"
Basic formatting with f-strings
An f-string evaluates expressions inside {}
and inserts the result. It’s concise and handles type conversion automatically.
123name = "Ada" tasks = 3 print(f"{name} completed {tasks} tasks.") # "Ada completed 3 tasks."
Numeric formatting (common cases).
1234price = 12.5 tax = 0.2 total = price * (1 + tax) print(f"Total: {total:.2f}") # 2 decimal places, e.g., "Total: 15.00"
If you need a literal brace, double it.
1print(f"Use {{}} for placeholders.") # "Use {} for placeholders."
For long loops that build big strings, collect pieces and then ''.join(pieces)
(you saw why in the immutability chapter).
1. You have items = ["red", "green", "blue"]
. Which is the best way to produce "red, green, blue"
?
2. What does "ha" * 2 + "!"
produce?
3. Which line prints a price with two decimal places if total = 7.5
?
Tak for dine kommentarer!