Concatenation and Repetition

Effectively manipulating strings is a core skill in programming, and understanding how to combine and duplicate them using operators is essential for tasks like formatting output, constructing messages, or generating data patterns.

Joining Strings with the '+' Operator

The concatenation operator, +, allows you to join two or more strings end-to-end, forming a single, longer string. This operation is fundamental for assembling dynamic text, such as creating full names from separate first and last name variables, or constructing file paths from directory and file components. Each string is appended in the order it appears in the expression.

pythonConcatenating String Literals and Variables
⚠️ Warning: Type Mismatch

The + operator requires both operands to be strings for concatenation. Attempting to concatenate a string directly with a number (integer or float) will result in a TypeError. You must explicitly convert numbers to strings using str() before concatenation, as shown in "Value: " + str(123).

Try It Yourself
Combine the item_name, quantity, and unit variables into a single string that reads "Order: 5 apples". Remember to add spaces where appropriate.
python

Repeating Strings with the '' Operator

The repetition operator, , allows you to duplicate a string a specified number of times. This is particularly useful for creating visual separators, padding, or generating repetitive patterns. You provide a string and an integer, and the operator returns a new string consisting of the original string repeated that many times.

pythonRepeating String Literals and Variables
⚠️ Warning: Repetition Factor Must Be Integer

The repetition operator requires the repetition factor to be an integer. Using a float (e.g., "abc" 2.5) or another string (e.g., "abc" "3") will result in a TypeError. The integer must also be non-negative; a zero or negative integer will produce an empty string.

Try It Yourself
Create a decorative line for a menu by repeating the star_char 30 times. Then, print the menu_title centered within two such lines.
python

New Strings, Not Modified Ones

An important characteristic of strings in Python is their immutability. This means that once a string object is created, its content cannot be changed. When you use the + or operators, you are not modifying the original string objects. Instead, these operations always produce a new string object in memory, leaving the original strings untouched. This behavior ensures consistency and predictability in string manipulation.

Check Your Understanding
What is the result of attempting to concatenate a string with an integer directly using the + operator in Python?
Key Takeaways
  • The + operator concatenates strings, joining them end-to-end to form a new string.

  • The operator repeats a string an integer number of times, creating a new string with the duplicated content.

  • Both + and operations on strings always produce new string objects; they do not modify existing strings due to string immutability.

  • Type consistency is critical: + requires string operands, and requires an integer repetition factor to avoid TypeError.

  • These operators are fundamental for dynamic text generation, formatting, and creating structured output in various programming tasks.

← All lessons in String Manipulation

Ready to keep this from fading?

Bitelrn turns lessons like this into a full course — quizzes, a knowledge map, and spaced review.

Get started free