Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Concatenation, Repetition, and Basic Formatting | Strings
Data Types in Python

bookConcatenation, 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.

1234567
first = "Hello" second = "world" msg = first + " " + second # "Hello world" score = 10 # "score: " + score # TypeError msg2 = "score: " + str(score) # OK: "score: 10"
copy

Repetition with *

Multiply a string by an integer to repeat it.

12
echo = "ha" * 3 # "hahaha" rule = "-" * 10 # "----------"
copy

Joining many pieces with "sep".join(...)

join is ideal when you have an iterable (like a list) of strings.

12345
words = ["Python", "is", "fun"] sentence = " ".join(words) # "Python is fun" lines = ["ID: 42", "OK", "Done"] block = "\n".join(lines) # "ID: 42\nOK\nDone"
copy

Basic formatting with f-strings

An f-string evaluates expressions inside {} and inserts the result. It’s concise and handles type conversion automatically.

123
name = "Ada" tasks = 3 print(f"{name} completed {tasks} tasks.") # "Ada completed 3 tasks."
copy

Numeric formatting (common cases).

1234
price = 12.5 tax = 0.2 total = price * (1 + tax) print(f"Total: {total:.2f}") # 2 decimal places, e.g., "Total: 15.00"
copy

If you need a literal brace, double it.

1
print(f"Use {{}} for placeholders.") # "Use {} for placeholders."
copy
Note
Note

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?

question mark

You have items = ["red", "green", "blue"]. Which is the best way to produce "red, green, blue"?

Select the correct answer

question mark

What does "ha" * 2 + "!" produce?

Select the correct answer

question mark

Which line prints a price with two decimal places if total = 7.5?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 5

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

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

bookConcatenation, Repetition, and Basic Formatting

Swipe to show menu

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.

1234567
first = "Hello" second = "world" msg = first + " " + second # "Hello world" score = 10 # "score: " + score # TypeError msg2 = "score: " + str(score) # OK: "score: 10"
copy

Repetition with *

Multiply a string by an integer to repeat it.

12
echo = "ha" * 3 # "hahaha" rule = "-" * 10 # "----------"
copy

Joining many pieces with "sep".join(...)

join is ideal when you have an iterable (like a list) of strings.

12345
words = ["Python", "is", "fun"] sentence = " ".join(words) # "Python is fun" lines = ["ID: 42", "OK", "Done"] block = "\n".join(lines) # "ID: 42\nOK\nDone"
copy

Basic formatting with f-strings

An f-string evaluates expressions inside {} and inserts the result. It’s concise and handles type conversion automatically.

123
name = "Ada" tasks = 3 print(f"{name} completed {tasks} tasks.") # "Ada completed 3 tasks."
copy

Numeric formatting (common cases).

1234
price = 12.5 tax = 0.2 total = price * (1 + tax) print(f"Total: {total:.2f}") # 2 decimal places, e.g., "Total: 15.00"
copy

If you need a literal brace, double it.

1
print(f"Use {{}} for placeholders.") # "Use {} for placeholders."
copy
Note
Note

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?

question mark

You have items = ["red", "green", "blue"]. Which is the best way to produce "red, green, blue"?

Select the correct answer

question mark

What does "ha" * 2 + "!" produce?

Select the correct answer

question mark

Which line prints a price with two decimal places if total = 7.5?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 5
some-alt