Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Concatenación, Repetición y Formato Básico | Cadenas de Texto
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Tipos de Datos en Python

bookConcatenación, Repetición y Formato Básico

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)
copy

Repetition with *

Multiply a string by an integer to repeat it.

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

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)
copy

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."
copy

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"
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).

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

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 5

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

bookConcatenación, Repetición y Formato Básico

Desliza para mostrar el menú

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)
copy

Repetition with *

Multiply a string by an integer to repeat it.

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

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)
copy

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."
copy

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"
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).

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

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 5
some-alt