A Guide on Python String Concatenation [with Examples]
By Rohit Sharma
Updated on Jun 02, 2025 | 17 min read | 76.3K+ views
Share:
For working professionals
For fresh graduates
More
By Rohit Sharma
Updated on Jun 02, 2025 | 17 min read | 76.3K+ views
Share:
Table of Contents
Did you know? Python wasn’t always the powerhouse programming language it is today! It started as a fun hobby project by Guido van Rossum in 1991, and here’s the twist—it's not named after the snake! Instead, it’s a tribute to the legendary British comedy group, Monty Python. Guido, a huge fan of Monty Python’s Flying Circus, wanted a name that was quirky, memorable, and a bit offbeat—just like the language itself! |
Python String concatenation means joining two or more strings into one continuous piece of text. It’s a basic yet essential operation used in numerous scenarios, such as building dynamic messages, combining user inputs, or formatting data for display.
For example, merging a user’s first and last name to form a full name, or creating URLs by joining base addresses with paths. This guide covers multiple ways to concatenate strings in Python, from simple operators to advanced methods, with clear examples to help you master this fundamental skill quickly and efficiently.
Python string concatenation means joining two or more strings end-to-end to create one continuous string. For instance, combining “Mumbai” and “Express” using Python string concatenation gives you “MumbaiExpress.” This technique is handy when creating names, titles, or any text that needs to be merged dynamically in your Python programs. The simplest way to do this is with the + operator, which directly links strings together.
Beyond basics, Python’s formatted strings (f-strings) let you combine variables smoothly. For instance:
name = "Aditi"
city = "Bangalore"
message = f"Hello, {name}! Welcome to {city}."
print(message)
This outputs:
Hello, Aditi! Welcome to Bangalore.
F-strings make Python string concatenation cleaner and more powerful, especially when working with dynamic data.
Want to excel in your Python skills? Take a look at upGrad’s Software Engineering courses and find the one that aligns with your interests and goals. Start learning and building your expertise.
Read: Python Project Ideas & Topics
Ready to start your Python programming journey? Join our Python Tutorial for Beginners 2025. Click below to get started and dive into Python programming today! Start Your Python Journey Now: What is Python | Basics of Python Programming | Python Tutorial for Beginners 2024 |
Python comprises a number of ways when it comes to concatenating or combining strings together. Since Python is an object-oriented programming language, everything in Python is an object. So, the new string that is created after Python concatenation is also referred to as a string object in Python.
Also Read: Data Science for Beginners Guide: What is Data Science?
Let us see the different ways by which we can concatenate strings in Python.
The simplest and most common method of concatenating a string is using the plus symbol (“+”). Let us see an example to understand it better:
a = “Python”
b = “is”
c = “cool”
print(a + b + c)
Output: Pythoniscool
Here, we have declared three string variables, “a”, “b,” and “c,” with three different string values. Then, we concatenate the three strings with the help of the “+” operator and display the output using the print statement. The output is the combination of the three strings .
You might use the “+” operator when you have a few strings to concatenate. This is because strings are immutable, i.e., they cannot be changed once created. So, for each concatenating statement, the interpreter creates a new object. Thus, it will be quite inefficient if you try to concatenate many strings using the “+” operator.
Another disadvantage of the “+” operator is that it does not allow any separator or delimiter between the strings. If you want to concatenate “Hello” and “World” with whitespace as a separator, you need to do something like this “Hello” + “ ” + “World,” and the output will be “Hello World”.
Ready to start coding? Enroll in the Basic Python Programming course by upGrad. In just 12 hours, you'll learn Python fundamentals, solve practical problems, and earn a certificate. Start today!
The asterisk (*) operator is used repeatedly when you want to concatenate the same string. For example, if you have a " red " string and want the same string to be concatenated three times, you use the * operator. The result will be “redredred”.
An example to illustrate the Python string concatenation using the “*” operator:
a = "Python"
print(a * 3)
Output: PythonPythonPython
Here, we have declared a single string variable “a” with a string value. Then, we concatenate the string with the help of the “*” operator and display the output using the print statement. The output combines the string with the same string three times.
Also Read: Big Data vs Data Analytics: Difference Between Big Data and Data Analytics
The join() method is the most flexible way of concatenating strings in Python. If you have many strings and you want to combine them together, use the join () method. It is a string method, and the most interesting thing about join() is that you can combine strings using a separator. It works on iterators like lists, tuples, strings, dictionaries, etc.
An example to illustrate the Python string concatenation of string using the “*” operator:
a = "Welcome"
b = "to"
c = "Python"
print(“-”.join([a,b,c]))
Output: Welcome-to-Python
Here, we have declared three string variables, “a”, “b,” and “c,” with three different string values. Then, we concatenate the three strings with the help of the join() method with “-” as a separator and display the output using the print statement. The output is the combination of the three strings together with the dash (“-”) operator in between the strings.
The modulus operator (“%”) can be used for string formatting and concatenation. It is useful for cases where you must combine strings and perform basic formatting.
An example to illustrate the concatenation of string using the “%” operator:
a = "Apple" b = "Shake" print(“% s % s” % (a, b))
Output: Apple Shake
Here, we have declared two string variables, “a” and “b,” with two different string values. Then, we concatenate the two strings with the help of the (“%”) and display the output using the print statement.
The “% s” denotes the string data type in Python, and the modulus (“%”) operator combines the string stored in the two variables “a” and “b”. The string value in the variables is passed to the string data type, and the output is displayed as the combination of two strings.
Improve your coding skills with upGrad’s Data Structures & Algorithms course. In 50 hours, learn essential concepts from arrays to advanced algorithms. Solve problems efficiently and get ready for technical interviews. Enroll now!
The str.format() function is a powerful function in Python that is also used for both String formatting and String Concatenation. This function combines different elements within a string through positional formatting.
An example to illustrate the concatenation of string using format() function:
a = "Virgin"
b = "Mojito"
print(“{} {}”.format(a, b))
Output: Virgin Mojito
Here, we have declared two string variables, “a” and “b” with two different string values. Then, we concatenate the two strings with the help of the format() function and display the output using the print statement.
The curly braces (“{}”) used here are used to fix the string position. The first variable is stored in the first curly braces and the second one in the second curly braces. The job of format() function is to concatenate the strings stored in variables “a” and “b” and display the combined string.
Formatted string literals or f-strings, in short, are string literals in Python. They contain an f at the beginning and curly braces that contain the expressions. It calls the str() method when an object argument is used as field replacement.
Let us see an example to illustrate the concatenation of string using f-string:
a = "Moscow"
b = "Mule"
print(f’{a} {b}‘)
Output: Moscow Mule
Here, we have declared two string variables, “a” and “b,”with two different string values. Then, we concatenate the two strings with the help of the f-string and display the output using the print statement.
The f-string expressions are evaluated at runtime, and they are formatted using the __format__ protocol in Python. It is considered a cleaner and easier way of Python string concatenation compared to the format() function.
Also Read: Types of Data Structures in Python: List, Tuple, Sets & Dictionary
Check out our free courses to get an edge over the competition.
String concatenation using StringIO is also a very flexible way of combining different strings in Python. In this method, we have to import the StringIO() function from the IO module.
An example to illustrate the concatenation of string using StringIO:
from io import StringIO
a = StringIO()
a.write(“Machine ”)
a.write(“Learning”)
print(a.getvalue())
Output: Machine Learning
Here, we have declared two string variables, “a” and “b” with two different string values. Then, we concatenate the two strings with the help of the StringIO() imported from the IO module and display the output using the print statement.
Here, the variable “a” acts as a file object in Python. The write() function is used here to write the string to the file, and the getvalue() function returns the entire content of the file.
We have covered all the ways by which we can concatenate different strings in Python. Let us see a few more miscellaneous examples to understand String Concatenation better.
There are various ways by which you can concatenate multiple strings in Python. The most common among them is using the plus (“+”) operator. You can combine both string variables and string literals using the “+” operator.
However, there’s another method that allows an easy way of concatenating multiple strings. It uses the in-place (+=) operator. The in-place operator concatenates the sequence with the right operand, and the result gets assigned to that sequence.
Let us see an example of Python string concatenation using the (“+=”) operator:
a = "Artificial "
b = "Intelligence"
a += b
print(a)
Output: Artificial Intelligence
Here, two string variables, “a” and “b” are declared with two different string values. The string on the right side of the “+=” operator is combined with the string variable on the left side. Then, the output is displayed using the print statement.
You can also add a string to the end of a string variable using the “+=” operator:
a = "Basket"
a += "ball"
print(a)
output: Basketball
Another way of concatenating multiple strings in Python is just by writing string literals consecutively:
a = "Red""Green""Blue"
print(a)
Output: RedGreenBlue
Also Read: Difference between Testing and Debugging
There are numerous ways of concatenating strings in Python. However, not all methods can concatenate strings and numbers. If you use the “+” operator to combine strings and numbers, it will raise errors. This is because strings can hold any recorded characters, but numbers like integers or floats are recorded as numerical values.
a = "Rolls Royce " b = 1948
print(a + b)
The error shows that the interpreter can concatenate a string value with another string value, but cannot concatenate a string value with an integer. However, you can overcome this problem with the help of the str() function in Python. It converts any integer or floating-point number into a string.
Let us see the same example with the str() function:
a = "Rolls Royce "
b = str(1948)
print(a + b)
Output: Rolls Royce 1948
The str() function converts the integer value 1948 into a string, and then it is concatenated with variable “a,” and the output is displayed using the print statement.
You can also use the format() function when you need to convert a number with decimal places or zero padding.
Also Read: What are the Advantages of Object-Oriented Programming?
You can concatenate a list of strings into one string using the join() method. It takes a character as a delimiter string. If you use an empty string as the delimiter, the list of strings will be simply concatenated without any separator.
Let us see an example of Python string concatenation using the join() function:
a = ["Apple", "Orange", “Banana”, “Mango”]
print(“\n”.join(a))
Output:
Apple
Orange
Banana
Mango
Here, the variable “a” is a list declared with four different string values. We have used newline (“\n”) as the delimiter in the join() method, which inserts a newline for each of the strings.
The output is the four strings, with each string in a new line.
You can use any other delimiter like comma (,) or hyphen (-) in the join() method and then perform Python string concatenation. Also, note that thejoin() method can also concatenate other iterators like tuples, sets, dictionaries, etc.
Ready to start your Python programming journey? Join our Python Tutorial for Beginners 2025. Click below to get started and dive into Python programming today! Start Your Python Journey Now: |
Python does not allow the concatenation of strings with numbers or numbers with numbers. However, you can convert a numeric value into a string using the str() method and then perform concatenation.
If you want to combine a list of numbers into one string, you first need to convert each integer in a list to a string using the str() function. Then, combine all the converted strings into a single string with the join() method.
Let us see an example to understand it better:
a = [1, 2, 3, 4, 5]
b = [str(a) for a in a]
print(“;”.join(b))
Output:
1;2;3;4;5
Here, the variable “a” is a list declared with five integer values. We convert each of the integers into a string using the str() function and store it in variable “b”. Then, we combine them together using the join() method with a colon (;) as the delimiter.
String formatting is a fundamental skill for any Python programmer, crucial for writing clear and maintainable code. Unlike simple concatenation with the + operator, which can be inefficient and awkward for complex strings, formatting methods like %, format(), and f-strings provide powerful, flexible ways to build dynamic text.
For example, generating customized email templates or logging detailed messages with variable data relies heavily on these techniques. They allow you to insert values cleanly, control number formatting, and improve code readability, making string formatting indispensable in real-world Python applications.
When to Use Which Method for String Formatting in Python:
Method |
Best For |
Example |
When to Avoid |
+ Operator | Concatenating a small number of strings. | a = "Python" + " is" + " cool" | When concatenating many strings, as it can be inefficient. |
* Operator | Repeating the same string multiple times. | a = "Python" * 3 | When you need to concatenate different strings or non-repetitive content. |
join() Method | Concatenating many strings with or without separators. | separator = "-"; result = separator.join([a, b, c]) | When working with a small number of strings. |
% Operator | Simple string formatting with placeholders. | a = "%s %s" % (name, city) | When working with modern Python versions, use f-strings instead. |
format() Function | Concatenating and formatting strings in a readable way. | message = "{} {}".format(name, city) | When needing simple, modern formatting like f-strings. |
f-string | Combining strings with variables in a clean and efficient way. | message = f"{name} is from {city}" | When not using Python 3.6+ or when simpler methods are enough. |
StringIO | Efficiently concatenating strings, especially in file-like operations. | from io import StringIO; a = StringIO(); a.write("Hello "); a.write("World") | When working with small text or not performing file operations. |
Now that you’ve seen the key methods for combining strings, let’s explore some practical tips to make your Python String concatenation cleaner, faster, and more effective.
Here are some key tips to help you optimize both the performance and readability of your Python string concatenation.:
1. Use the % Operator for Simplicity:
The % formatting operator is fast and effective for concatenating a few strings. A key advantage is that you don’t need to manually convert numbers to strings, as the operator handles this implicitly. It also keeps your code clean and improves readability.
2. Leverage the join() Method for Efficiency:
When you need to concatenate multiple strings, especially from a sequence like a list or tuple, the join() method is the most efficient and readable option. It avoids the inefficiency of repeatedly creating new string objects, making it ideal for handling large numbers of strings.
3. Use Lists for Non-Sequential Data:
If you're working with many strings that come from different sources (e.g., user input or computations) and they’re not in a sequence, it’s best to store them in a list. You can then apply join() to concatenate them all together. For example, use list comprehension or append() to collect the strings before joining them, ensuring your code remains organized.
4. Avoid Using + for Large Concatenation:
While the + operator is simple to use, it’s inefficient for concatenating many strings, especially inside loops. Since strings are immutable, each concatenation creates a new string object, which can result in performance issues.
5. Consider f-strings for Readability:
When concatenating strings with variables, f-strings (available in Python 3.6 and above) offer the cleanest and most readable solution. They allow you to embed variables directly within the string, making your code both concise and easy to understand.
To wrap things up, understanding Python string concatenation is essential for efficient programming. Whether you're joining a few strings or handling larger data, selecting the right method can significantly enhance your code's performance and readability. From the simplicity of the + operator to the power of join() and f-strings, the right choice depends on the task at hand.
Choosing the optimal approach can be tricky, and many developers face challenges in making the right decision. UpGrad’s Python courses can help you refine your skills and gain practical experience with string manipulation and other key concepts.
In addition to the courses mentioned earlier, you can explore these options:
Want to advance your Python skills? upGrad’s expert counselors are ready to guide you toward the best learning path for your career. You can also visit your nearest offline center; we’re here to offer the support you need to move ahead with confidence. Enroll today!
Unlock the power of data with our popular Data Science courses, designed to make you proficient in analytics, machine learning, and big data!
Elevate your career by learning essential Data Science skills such as statistical modeling, big data processing, predictive analytics, and SQL!
Stay informed and inspired with our popular Data Science articles, offering expert insights, trends, and practical tips for aspiring data professionals!
Reference:
https://pwskills.com/blog/facts-about-python/
763 articles published
Rohit Sharma shares insights, skill building advice, and practical tips tailored for professionals aiming to achieve their career goals.
Get Free Consultation
By submitting, I accept the T&C and
Privacy Policy
Start Your Career in Data Science Today
Top Resources