Yhdistäminen, Toisto ja Perusmuotoilu
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.
12345echo = "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.
1print(f"Use {{}} for placeholders.") # "Use {} for placeholders."
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?
Kiitos palautteestasi!
Kysy tekoälyä
Kysy tekoälyä
Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme
Mahtavaa!
Completion arvosana parantunut arvoon 3.45
Yhdistäminen, Toisto ja Perusmuotoilu
Pyyhkäise näyttääksesi valikon
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.
12345echo = "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.
1print(f"Use {{}} for placeholders.") # "Use {} for placeholders."
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?
Kiitos palautteestasi!