Concatenating Strings in Python
Learn how to concatenate Python strings with +, add spaces and punctuation, convert integers with str(), and fix common TypeError problems.
String concatenation means joining separate strings into one new string. In Python, the + operator joins string values. For example, "Good" + " morning" produces "Good morning".
A string is a sequence of text characters, usually written inside single or double quotation marks. A variable is a named reference that stores a value, such as text or a number.
What String Concatenation Means
Concatenation is the process of joining strings into one string. Python uses + as the concatenation operator when both operands are strings.
first = "Hello"
second = "Python"
combined = first + second
print(combined)Output:
HelloPythonThe result is a new string containing the characters from first, followed immediately by the characters from second.
The same operator can have a different meaning with numbers. With integers, + performs numeric addition:
total = 10 + 5
print(total)Output:
15Therefore, + does not always mean concatenation. Its behavior depends on the types of the values being combined.
Concatenating String Variables
Text fragments can be stored in separate variables and then joined. This is useful when each part of the final message comes from a different value.
given_name = "Amina"
surname = "Khan"
full_name = given_name + surname
print(full_name)Output:
AminaKhanThe assignment to full_name stores the resulting combined value. Calling print(full_name) displays it.
Spaces and Punctuation Are Not Added Automatically
Direct concatenation joins characters exactly as they are supplied. Python does not automatically add spaces, punctuation, or line breaks.
In the previous example, given_name + surname produced "AminaKhan" because neither variable contains a separating space. A quoted value such as " " is a string literal: text written directly in code inside quotation marks.
given_name = "Amina"
surname = "Khan"
full_name = given_name + " " + surname
print(full_name)Output:
Amina KhanThe expression has three parts: the given name, a one-space string literal, and the surname. You can include any intended separator explicitly.
label = "Name: " + full_name
message = "Hello, " + full_name + "!"
lines = "First line\nSecond line"In these examples, the space after "Name: ", the comma and space after "Hello, ", the exclamation mark, and the line-break escape sequence are all supplied by the programmer.
Concatenation Inputs and Results
Concatenating Strings with Numeric Values
An integer is a whole-number value, such as 30. An integer cannot be joined directly to a string with +.
name = "Amina"
age = 30
message = name + " is " + age
print(message)This raises a TypeError. A TypeError occurs when an operation is attempted with incompatible data types. Here, Python is asked to concatenate strings and an integer:
TypeError: can only concatenate str (not "int") to strUse str() to convert the integer to its text representation before concatenating it. This is a form of type conversion, meaning that a value is changed from one data type to another.
name = "Amina"
age = 30
message = name + " is " + str(age) + " years old."
print(message)Output:
Amina is 30 years old.str(age) produces the string "30". The resulting expression contains only strings, so every use of + performs string concatenation.
Common Value Types in String Concatenation
Reading and Correcting Concatenation Output
When checking a concatenated result, inspect both the characters and the order of the components.
- Look for missing spaces: If words run together, add a space literal such as
" ". - Check the types: Every value joined with
+must be a string. Convert an integer withstr(). - Check the order: Python preserves the order of the operands.
"A" + "B"gives"AB", while"B" + "A"gives"BA". - Check punctuation: Commas, periods, exclamation marks, and other separators must be included explicitly.
first = "Ada"
last = "Lovelace"
age = 36
sentence = first + " " + last + " is " + str(age) + " years old."
print(sentence)Output:
Ada Lovelace is 36 years old.Troubleshooting Common Problems
Words appear joined together
Problem: The output is AminaKhan instead of Amina Khan.
Cause: The expression does not contain a space string between the values.
Fix: Add " " between the variables:
full_name = given_name + " " + surnameA TypeError occurs with an age
Problem: A sentence combines text with an integer and raises TypeError.
Cause: A string and an integer are being used with +.
Fix: Convert the integer:
message = "Age: " + str(age)The sentence is in the wrong order
Problem: The output contains the right values but in an unintended sequence.
Cause: The variables and string literals are arranged in the wrong order.
Fix: Reorder the components in the expression. For example, first + " " + last places the given name before the surname, while last + ", " + first places the surname first.
Key Points
- String concatenation joins strings into one new string.
- Use
+between string values to concatenate them. - Spaces, punctuation, and line breaks are not inserted automatically.
- Use a string literal such as
" "when a separator is needed. - Convert an integer with
str()before joining it to text. - The order of operands determines the order of the final output.
For larger messages, you may later find string formatting techniques useful as well.