How to concatenate string in python?

String concatenation is the operation of joining two or more strings. Strings are immutable. Therefore, when two strings are combined the result must be assigned to a new variable.

For example, let us consider string1 = "Daniel" and string2="Defoe". The concatenation operation of string1 and string2 returns the output as "DanielDefoe".

string1 = "Daniel"
string2 = "Defoe"
string3 = string1 + string2
print(string3)

The output of string concatenation

DanielDefoe

There are different ways to perform string concatenation. Some of them are as follows.

Concatenation using the '+' operator

The '+' operator is used to combine multiple strings together. The arguments must be a string. For example, let us consider string1 = "Python" and string2 ="Programming". The '+' operator concatenates two strings and gives output as "PythonProgramming".

string1 = "Python"
string2 = "Programming"
string3 = string1 + string2
print("concatenation of string without space:",string3)
string4 = string1 + " " + string2
print("concatenation of string with space:",string4)

Output

concatenation of string without space: PythonProgramming
concatenation of string with space: Python Programming

Concatenation using the .join() method

join() method returns a string by concatenating the string elements in the iterable. The string elements are joined by separator sep. The syntax for the join method is as follows.

#syntax: 
sep.join([str1, str2,......, strn])

Here, str1,str2,...........strn represents a list of strings. A list is used as an argument because the join method accepts only one argument.

For example, let us consider string1 = "Python" and string2 ="Programming" and represent it in the form of a list as [string1,string2]. "".join([string1,string2]) performs the joining operation and gives output as "PythonProgramming".

string1 = "Python"
string2 = "Programming"
string3 = "".join([string1,string2])
print("joining of string without space:",string3)
string4 = " ".join([string1,string2])
print("joining of string with space:",string4)

Output

joining of string without space: PythonProgramming
joining of string with space: Python Programming
In the above two methods, if a non-string value is passed as an argument it raises a TypeError Exception

Example,

string1 = "Python"
string2 = 3
string3 = string1 + string2
print("concatenation of string:",string3)

Output

string3 = string1 + string2
TypeError: can only concatenate str (not "int") to str

Concatenation using format() function

In python 3 the format() function is used for string formatting in a more efficient way. string.format() method is used to replace the placeholders with the specified strings present inside the format function. The placeholder is represented with a pair of curly braces {}.

The syntax for format() method is as follows:

#syntax:
string.format(var1, var2......., varn)

For example, let us consider string1 = "Python" and string2 ="Programming". and the string as {}{} the string.format(string1,string2) gives output as "PythonProgramming".

string1 = "Python"
string2 = "Programming"
string3 = "{}{}".format(string1,string2)
print("formatting of string without space:",string3)
string4 = "{} {}".format(string1,string2)
print("formatting of string with space:",string4)

Output

formatting of string without space: PythonProgramming
formatting of string with space: Python Programming
0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions