Question
How to Concatenate Strings in Python: str and String-Like Values
Question
In Python, how can I concatenate these combinations of text values?
strandstrStringandstrStringandString
I want to understand the correct way to join string values together, especially when the types may not appear identical.
Short Answer
By the end of this page, you will understand how string concatenation works in Python, how to join normal string values using +, what to do when values are not both plain str, and how to avoid common type-related mistakes.
Concept
In Python, string concatenation means combining text values into a single string.
The most common way to concatenate two strings is with the + operator:
first = "Hello, "
second = "world"
result = first + second
print(result)
This produces:
Hello, world
The important rule
Both values must be strings for + to work directly in string concatenation.
Python's built-in text type is str. So when you concatenate text in Python, you are usually working with str objects.
a = "foo"
b = "bar"
print(a + b) # foobar
What about String?
In Python, String with a capital S is not a built-in type like it is in some other languages such as Java or C#. The real built-in string type is str.
Mental Model
Think of a Python string as a strip of text on paper.
- Concatenation means taping two strips together end to end.
- If both pieces are already text, Python can join them easily.
- If one piece is not actually text, Python first needs you to turn it into text.
So:
str + str→ tape works immediately- non-
str+str→ convert first, then tape
If you try to tape a number or custom object directly onto a text strip, Python stops and asks you to be explicit.
Syntax and Examples
Basic syntax
result = string1 + string2
Both string1 and string2 should be str values.
Example: str and str
first_name = "Ada"
last_name = "Lovelace"
full_name = first_name + " " + last_name
print(full_name)
Output:
Ada Lovelace
This works because all parts are strings.
Example: converting another type to str
age = 28
message = "Age: " + str(age)
print(message)
Output:
Age: 28
Without str(age), Python would raise an error because is an integer, not a string.
Step by Step Execution
Consider this example:
class String:
def __init__(self, value):
self.value = value
def __str__(self):
return self.value
left = String("Hello")
right = " there"
result = str(left) + right
print(result)
Step by step
- A custom class named
Stringis defined. - The
__init__method stores the text inself.value. - The
__str__method tells Python how to turn the object into a normal string. left = String("Hello")creates aStringobject.right = " there"creates a normal Pythonstr.str(left)callsleft.__str__()and returns"Hello".
Real World Use Cases
String concatenation is used constantly in real programs.
Building messages
username = "maria"
message = "Welcome, " + username
Used in command-line tools, web apps, and notifications.
Creating log output
status = "OK"
log_line = "Service status: " + status
Useful for debugging and monitoring.
Combining file name parts
name = "report"
extension = ".txt"
filename = name + extension
Common in scripts that generate files.
Formatting data for display
city = "Paris"
country = "France"
label = city + ", " + country
Used in UI labels, exports, and reports.
Joining API or database values
first = "Grace"
last = "Hopper"
full = first + " " + last
Useful when displaying structured data as readable text.
Real Codebase Usage
In real projects, developers often concatenate strings in a few common ways.
Simple + for small, direct cases
error_prefix = "Error: "
message = error_prefix + "invalid input"
This is fine when combining only a small number of string values.
F-strings for readability
user = "alice"
count = 3
text = f"User {user} has {count} notifications"
This is very common in production code because it is clear and concise.
''.join(...) for many pieces
parts = ["Py", "thon", "3"]
result = "".join(parts)
This is preferred when joining many strings from a list or loop.
Validation before concatenation
Developers often check values before building strings:
def greet(name):
if name is :
+ (name)
Common Mistakes
1. Concatenating a string with a non-string
Broken code:
age = 30
text = "Age: " + age
This raises:
TypeError
Fix:
text = "Age: " + str(age)
2. Assuming String is a built-in Python type
Broken idea:
value = String("hello")
This fails unless String was defined somewhere.
In Python, the built-in string type is:
str
3. Forgetting spaces when concatenating
first = "Hello"
second = "World"
print(first + second)
Output:
HelloWorld
Comparisons
| Approach | Best for | Example | Notes |
|---|---|---|---|
+ | Joining a small number of strings | "a" + "b" | Simple and readable for short cases |
str() + + | Mixed types | "Age: " + str(age) | Explicit conversion avoids errors |
| f-strings | Readable formatting | f"Age: {age}" | Usually the clearest choice |
"".join(...) | Many strings in a list | "".join(parts) | Better for repeated joining |
Cheat Sheet
Quick rules
- Python's built-in string type is
str - Concatenate strings with
+ - Both sides of
+must be strings for string concatenation - Use
str(value)to convert non-string values - Use f-strings for cleaner mixed-value formatting
- Use
"".join(list_of_strings)for many string parts
Common patterns
"Hello" + "World"
"Hello " + name
"Age: " + str(age)
f"Age: {age}"
"".join(["a", "b", "c"])
If you see String
- It is a built-in Python type
FAQ
Is String a built-in type in Python?
No. Python's built-in text type is str. String is not a standard built-in type.
How do I concatenate two strings in Python?
Use the + operator:
result = "Hello" + "World"
Why do I get a TypeError when using +?
Because one of the values is not a string. Convert it first with str(...) or use an f-string.
Should I use + or f-strings?
Use + for very simple string-to-string joining. Use f-strings when combining text with variables or other types.
What is the best way to join many strings?
Use "".join(...) when you have a list of strings.
Can custom objects be concatenated like strings?
Not directly unless they behave like strings. Usually you should convert them with str(obj) first.
Does Python automatically convert numbers to strings during concatenation?
Mini Project
Description
Build a small Python program that creates user-friendly profile messages by combining text values. This demonstrates plain string concatenation, converting non-string values, and working with a custom string-like object.
Goal
Create formatted profile output using str, +, and str(...) conversion safely.
Requirements
- Create a normal Python string for a user's first name
- Create an integer value for the user's age
- Define a custom
Stringclass with a__str__method - Use concatenation to build one complete profile message
- Print the final message without raising a type error
Keep learning
Related questions
Accessing Cargo Package Metadata in Rust
Learn how to read Cargo package metadata like version, name, and authors in Rust using compile-time environment macros.
Default Function Arguments in Rust: What to Use Instead
Learn how Rust handles default function arguments, why they are not supported, and practical patterns to achieve similar behavior.
Fixing Rust "linker 'cc' not found" on Debian in WSL
Learn why Rust shows "linker 'cc' not found" on Debian in WSL and how to fix it by installing the required C build tools.