How to concatenate strings in Python?
What is a String in Python?
As with any other programming language, a string is a sequence of characters. For example, “Hello, World” consists of 12 (H: e: l: l:o:,: :W:o:r:l:d] characters.
It is possible to define variables in a program that are of type string that will be objects of the String class. The String class contains inbuilt methods and operators for manipulating strings.
What is concatenation means?
The process of combining two or more strings to build another string is known as concatenation. For example, “Hello” and “World” are the two strings. After concatenation, the new string will be HelloWorld.
What are the methods for concatenating strings in Python??
Appending strings can be accomplished in the following ways.
- Using + operator
- Using Format inbuilt function.
- With Join inbuilt function.
- With % operator
- Using a comma (,)
A detailed explanation of each above option will be provided in this tutorial.
Using the + operator:
The simplest way is to use a + operator. It takes two string operands and combines them. If a programmer wants to append multiple strings, he has to use the + operator multiple times. The following examples show how to append two strings in Python using a + operator.
#Program to demonstrate
#How to use + Operator for concatenation
string_1 = "Hello";
string_2 = "World";
#Concatenate Two strings
concatenated_string = string_1 + string_2;
print(concatenated_string);
HelloWorld
The following example demonstrates how to concatenate multiple strings using the + operator.
#Program to demonstrate
#How to use + Operator for multiple string concatenation
string_1 = "Hello";
string_2 = "World";
string_3 = " How are you?";
#Concatenate Two strings
concatenated_string = string_1 + string_2 + string_3;
print(concatenated_string);
HelloWorld How are you?
Using The string Format function:
Format is a standard method of the string class in Python. It takes the strings and a format specifier to place them at placeholders and how they are displayed on the output. In the following example, we will use the basic format specifier to concatenate two strings. A programmer can use the format function to concatenate more than two strings, use a list of strings, etc.
#Program to demonstrate
#How to use string format method for string concatenation
string_1 = "Hello";
string_2 = "World";
#Concatenate Two strings
concatenated_string = "{} {}".format(string_1, string_2);
print(concatenated_string)
Hello World
In the above example, a string starts with ” and ends with “. Inside that, we have two {}. The {} is a format specifier. In the actual output, the {} replaces the corresponding string in the input list. The first string inside the format function substitutes the first {}, the other string replaces the second {}, and so on.
In output, there is a space between two strings, although we have not put any string for that. It is because there is space between two {}.
Using the string join() method.
The Python string has another method named join(). This function takes the sequence of elements as an argument and builds a string using a separator string. A separator could be a single character, a small string, or nothing. The following examples show how to use the join() function to concatenate two strings. A similar approach will also work for more than two strings.
#Program to demonstrate
#How to use string join method for string concatenation
string_1 = "Hello";
string_2 = "World";
#Concatenate Two strings
concatenated_string = "{} {}".format(string_1, string_2);
# No separator
print("".join([string_1,string_2]));
#Comma as a separator
print(",".join([string_1,string_2]));
#Space as a separator
print(" ".join([string_1,string_2]));
HelloWorld
Hello,World
Hello World
In the above example, the separator is a string on which the join() method is called.
Using % Python Operator:
In Python, % is an operator. While for string, it could work as a format specifier. It’s very simple to use. The following example demonstrates how to use % to concatenate two strings.
#Program to demonstrate
#How to use string % string concatenation
string_1 = "Hello";
string_2 = "World";
#Concatenate Two strings using %
newstring = "% s % s" %(string_1, string_2);
print(newstring);
Hello World
Using a comma(,):
This is the simplest way to display more than one string in concatenated way. In the print function, you can pass multiple strings separated by commas. The output will contain a bigger string with whitespace.
Not good if you want to store a bigger string in another variable or you do not want to add white spaces between strings.
You might already use the print function with commas for display values. Here is just to show how it concatenates two strings.
#Program to demonstrate
#How comma string concatenation
string_1 = "Hello";
string_2 = "World";
#Concatenate Two strings using comma
newstring = string_1, string_2;
print(string_1,string_2);
Hello World
Conclusion –
Throughout this tutorial, we have learned multiple ways to concatenate strings in Python. Each method has a different approach. The approach that you use will depend on the purpose of your concatenation.