In this python tutorial, we will learn how to append or concatenate strings.
Table Of Contents
Introduction
Suppose we have two strings,
"Welcome to " "thispointer"
Now we want to append or concatenate these two strings. Final string should be like,
Welcome to thispointer
There are different ways to do this. Let’s discuss some approaches to append or concatenate strings in Python.
Frequently Asked:
Append Strings in Python using + operator
The + operator is used to combine two or more strings in Python without any separator.
Syntax:
input_str1 + input_str2 +.............
where input_str1 and input_str2 are the strings.
Example 1:
Latest Python - Video Tutorial
In this example, we will concatenate two strings using the + operator.
input_str1 = "Welcome to " # Display the actual string print(input_str1) input_str2 = "thispointer" # Display the actual string print(input_str2) # Concate the two strings new_str = input_str1 + input_str2 print("Concatenated String: ", new_str)
Output:
Welcome to thispointer Concatenated String: Welcome to thispointer
We can see that two strings are concatenated without any separator.
Example 2:
In this example, we will concatenate three strings using the + operator.
input_str1="Welcome" # display the actual string print(input_str1) input_str2="to" # display the actual string print(input_str2) input_str3="thispointer" # display the actual string print(input_str3) # Concate three strings new_str = input_str1 + input_str2 + input_str3 # concatenate three strings print("Concatenated String: ", new_str)
Output:
Welcome to thispointer Concatenated String: Welcometothispointer
We can see that three strings are concatenated without any separator.
Append Strings in Python using %s
The %s in a string specifies the string variables. It can be used to combine two or more strings. WIth this approach, it is possible to separate the combining strings with any text.
Syntax:
"%s%s" % (input_str1, input_str2)
where input_str1 and input_str2 are the strings.
Example 1:
In this example, we will concatenate two strings using the %s.
input_str1="Welcome to " # display the actual string print(input_str1) input_str2="thispointer" # display the actual string print(input_str2) # concatenate two strings new_str = "%s%s" % (input_str1, input_str2) print("Concatenated String: ", new_str)
Output:
Welcome to thispointer Concatenated String: Welcome tothispointer
We can see that two strings are concatenated.
Example 2:
In this example, we will concatenate three strings using %s.
input_str1="Welcome" # display the actual string print(input_str1) input_str2="to" # display the actual string print(input_str2) input_str3="thispointer" # display the actual string print(input_str3) # concatenate three strings new_str = "%s%s%s" % (input_str1,input_str2,input_str3) print("Concatenated String: ", new_str)
Output:
Welcome to thispointer Concatenated String: Welcometothispointer
We can see that three strings are concatenated.
Append strings using join()
The join() is used to join two or more strings present in a list. It takes input_str which is a list of strings, that needs to be concatenated.
Syntax:
"delimiter".join(input_str)
where input_str is a list of strings and delimiter is the separator between the concatenated strings.
Example 1:
In this example, we will concatenate two strings in a list using join().
input_str=["Welcome to", "thispointer"] # display the actual string print(input_str) # concate the strings in list new_str = "-".join(input_str) print("Concatenated String: ", new_str)
Output:
['Welcome to', 'thispointer'] Concatenated String: Welcome to-thispointer
We can see that two strings are concatenated with “-” as the delimiter.
Example 2:
In this example, we will concatenate three strings using join().
input_str=["Welcome", "to","thispointer"] # display the actual string print(input_str) # concate the strings in list new_str = " ".join(input_str) print("Concatenated String: ", new_str)
Output:
['Welcome', 'to', 'thispointer'] Concatenated String: Welcome to thispointer
We can see that three strings are concatenated with space (” “) as the delimiter.
Append strings using comma(,)
We can directly concatenate the input strings in the print() statement separated by a comma.
Syntax:
print(input_str1, input_str2,............)
Where input_str1 and input_str2 are strings.
Example 1:
In this example, we will concatenate two strings using print().
input_str1="Welcome to" # display the actual string print(input_str1) input_str2="thispointer" # display the actual string print(input_str2) # concatenate two strings print("Concatenated String: ",input_str1,input_str2)
Output:
Welcome to thispointer Concatenated String: Welcome to thispointer
We can see that two strings are concatenated.
Example 2:
In this example, we will concatenate three strings.
input_str1="Welcome" # display the actual string print(input_str1) input_str2="to" # display the actual string print(input_str2) input_str3="thispointer" # display the actual string print(input_str3) # concatenate three strings print("Concatenated String: " ,input_str1,input_str2,input_str3)
Output:
Welcome to thispointer Concatenated String: Welcome to thispointer
We can see that three strings are concatenated.
Summary
In this string tutorial, we discussed 4 ways to concatenate two or multiple strings. The + operator directly concatenated multiple strings without any separator. The %s operator can used to join strings with some text in between and after the strings. The join() function can be use to join a list of strings. Lastly, we concatenated the strings directly using the print() statement and with the comma operator. Happy Learning.
Latest Video Tutorials