How to concatenate strings in python in multiple ways?
What is a String? It is a sequence of characters. For example “Hello, World” is a string having 12 characters. The purpose of the string is to associate a meaningful name to the functions, variables, etc.
In Python, we can create a variable of type string that will be an object of the String class. The string class has inbuilt functions and operators for the user.
What is concatenation? The process to combine more than one string together to form a single bigger 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 ways to concatenate strings in python?
There are the following ways to append strings.
- Using + operator
- Using Format inbuilt function.
- With Join inbuilt function.
- With % operator
- Using a comma (,)
In this tutorial, we will explain each option with an example.
Concatenate using + operator:
This is the simplest way. The + operator takes two string operands and combines them together. If you need to append multiple strings you need to call + operator multiple times. The following examples show how to use the + operator for strings in Python.
#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);
Output->
HelloWorld
Append 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);
Output:
HelloWorld How are you?
Using Format function:
Format a standard function of the string class in python. It takes the values and a format specifier to place the values at locations and the way displays on the output. In the following example, we will use the simplest format specifier to concatenate two strings. You 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)
Output:
Hello World
In the above example string starts with ” and ends with “. Inside that, we have two {}. The {} is the format specifier, in the actual output the {} replaces the string. The first string inside the format function, replaces the first {}, the second sting second {}, and so on.
If 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 (“,”, “.” etc), a small string, or nothing. The following examples show how to use the join() function two concatenate two strings. A similar approach will also work on 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]));
Output:
HelloWorld Hello,World Hello World
In the above example, the separator is a string on which the join() method is called.
Using %
In python, % is an operator. While for string it could work as a format specifier. It’s very simple to use, in the following example we are demonstrating 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);
Output:
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);
Output:
Hello World
Conclusion –
In this tutorial, we have learned multiple ways to concatenate the string in python. Each has its own way to do that. What you can use depends on the use case.