Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Concatenation, Repetition, and Basic Formatting | Section
Working with Numbers in Python: Integers, Floats, and Type Conversion - 1769704232138

Concatenation, Repetition, and Basic Formatting

Swipe um das Menü anzuzeigen

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.

1234567891011
# Creating a personalized game message player_first = "Alice" player_last = "Johnson" greeting = player_first + " " + player_last # "Alice Johnson" score = 10 # "Score: " + score # TypeError → number must be converted to string score_message = "Score: " + str(score) # "Score: 10" print(greeting) print(score_message)

Repetition with *

Multiply a string by an integer to repeat it.

12345
echo = "ha" * 3 # "hahaha" rule = "-" * 10 # "----------" print(echo) print(rule)

Joining Many Pieces with "sep".join(...)

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

123456789
# Building a message and a log entry from list data message_parts = ["Welcome", "to", "Codefinity!"] welcome_message = " ".join(message_parts) # "Welcome to Codefinity!" log_lines = ["User ID: 42", "Status: OK", "Process: Done"] log_block = "\n".join(log_lines) # "User ID: 42\nStatus: OK\nProcess: Done" print(welcome_message) print(log_block)

Basic Formatting with f-strings

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

1234
# Displaying a progress message for a team member name = "Ada" tasks = 3 print(f"{name} completed {tasks} tasks.") # "Ada completed 3 tasks."

Numeric formatting (common cases).

123456
# Calculating the total cost of an online purchase item_price = 12.5 tax_rate = 0.2 total_cost = item_price * (1 + tax_rate) print(f"Total to pay: ${total_cost:.2f}") # Rounded to 2 decimal places, e.g. "Total to pay: $15.00"

If you need a literal brace, double it.

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

For long loops that build big strings, collect pieces and then ''.join(pieces).

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"?

Wählen Sie die richtige Antwort aus

question mark

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

Wählen Sie die richtige Antwort aus

question mark

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

Wählen Sie die richtige Antwort aus

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 17

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Abschnitt 1. Kapitel 17
some-alt