How to Concatenate Strings in Python?

In this Python tutorial, we will learn about different methods to concatenate strings.

Table Of Contents

What is String Concatenation and Why do we need to concatenate strings ?

String Concatenation means adding two strings to create a new string. Like we may have a user input form, in which we take two inputs, like First Name and Second Name. But we need to display the Full Name. In this kind of scenario we use String Concatenation, which combines first name and second name to form a Full name.

Now lets learn about some methods through which we can Concatenate Strings in Python Programming Language.

Advertisements

Concatenate Strings using format() method

First method that we can use to concatenate strings is the format() method of class string. It is used for complex variable substitutions and value formatting. Substitution of values are done using curly braces({}), which is also known as placeholders. It returns the formatted string and takes 2 arguments :

Syntax :

str.format(value,[,format_spec])
  • value : value which needs to be concatenated in string.
  • format_spec : how value should be formatted. Optional

It returns a formated string. See the example code below

CODE :

# initialized first string which will be passed 
# as the first value in format() method
language_name = 'Python Programming language'

# Initialized second string which will be passed 
# as the second value in format() method
use = 'String Concatenation'

# concatenating string using the format() method 
merged_str = 'We will be using {0} for {1}'.format(language_name, use)

print(merged_str)

OUTPUT :

We will be using Python Programming language for String Concatenation

In the above code, we have used the str.format() method and successfully concatenated variables language_name and use to create a new string. You can also use index number of values provided as an argument with curly braces to insert variables at your desired place. This method also raises a ValueError if number of curly braces is more than of the number of values provided in argument.

Concatenate Strings using join() method

Next method that we can use to concatenate the strings is the join() method of class string. It accepts an iterable as argument and returns a single string joining the elements in the iterable. This method raises a TypeError if the elements of given iterable are not of string type. See the example code below.

CODE :

# initialized first string
firstStr = 'This is a Python tutorial'

# initialized second string
secondStr = 'on thispointer.com'

# concatenating first string and second string
# into final string using join() method
finalStr = ' '.join([firstStr, secondStr])

print(finalStr)

OUTPUT :

This is a Python tutorial on thispointer.com

So in the above example code and output, we passed a list of strings as an argument to the join() method. It returned a string by concatenating the strings in list. We also provides a single whitespace as separator. Strings in list will be joined using this single whitespace as separator. It you want to spaces in between the two strings while joining, then you can also an empty space as a separator.

Concatenate Strings using % Operator

Next method that we can use to concatenate string is by the % operator. It is also known as modulo operator or string-formatting operator, and mainly used for string formatting. We need to provide specifiers like “%s” for string , “%d” for integers , “%f” for float. It also raises a TypeError if provided specifier is not present. For example, suppose we provided “%d” and do not provided any integer, then it will throw a TypeError. See the Example Code below :

# initialized first string
str_1 = 'This is a Python tutorial'

# initialized second string
str_2 = 'on thispointer.com'

# using % to concatenate str_1 and str_2
joined_str = "%s %s" %(str_1,str_2)

print(joined_str)

OUTPUT :

This is a Python tutorial on thispointer.com

So in the above example, we have used modulo operator concatenate str_1 and str_2 to print final string with proper specifier (%s for string). Using this we can also merge int values along with the strings. For example,

# initialized first string
str_1 = 'There are more than'

# An int value
count = 1000

# initialized second string
str_2 = 'articles.'

# using % to concatenate str_1 and str_2
joined_str = "%s %d %s" % (str_1, count, str_2)

print(joined_str)

Output:

There are more than 1000 articles.

Concatenate Strings using + operator

Next method, that we can use is the “+” operator, which is simply an addition operator. In this method we will add two strings and store it in the third variable. See the example code below :

# initialized first string
str_1 = 'This is a Python tutorial'

# initialized second string
str_2 = 'on thispointer.com'

# concatenating str_1 and str_2 
# using + and storing it in final_str
final_str = str_1 + str_2 

print(final_str)

OUTPUT :

This is a Python tutorialon thispointer.com

In above example, we have used + operator to concatenate two string str_ 1 and str_2. This is one of the simplest method.

Concatenate Strings using comma operator

Another super easy method we can use is ,(comma) to concatenate strings. See the example code below :

# initialized first string
str_1 = 'This is a Python tutorial'

# initialized second string
str_2 = 'on thispointer.com'

# concatenating str_1 and str_2 using , 
print(str_1, str_2)

OUTPUT :

This is a Python tutorial on thispointer.com

In the above example, we used comma operator with a whitespace to concatenate str_1 and str_2. This is another super easy method and can be used to concatenate string.

Summary

So in this python tutorial, we learned about five different methods through which we can concatenate the given strings. If the given strings are just two different variables and need to be concatenated in a single one then we can use Method 4 and Method 5 because they are super easy to understand and write. But if there is complex concatenation like on some specified postion inside another string with different types of data types then the best method can be Method 1.

Also we have used Python 3.9.12 for writing example codes. Type python –version to check your python version. Always try to write example codes on your machine. Happy Coding.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top