In Python, Strings are arrays of bytes representing Unicode characters. However, Python does not have a character data type, a single character is simply a string with a length of “1”. Square brackets [ ] can be used to access elements of the string. In this article, we will learn how to concat strings in Python using different methods.
Example:
Input: string1, string2
Output: "string1 string2"
Explanation: In this example, we have passed two strings as input, and we get the single string as output by joining them together.
Concatenate Strings in Python
String Concatenation is the technique of combining two strings. String Concatenation can be done using different ways as follows:
- Using + operator
- Using join() method
- Using % operator
- Using format() function
- Using “,” (comma)
Python String Concatenation using the ‘+’ Operator in Python
It’s very easy to use the + operator for string concatenation. This operator can be used to add multiple strings together. However, the arguments must be a string. Here, The + Operator combines the string that is stored in the var1 and var2 and stores in another variable var3.
Note: Strings are immutable, therefore, we don’t modify the string instead we concatenate them together and assigned them to a new variable.
Python3
var1 = "Hello "
var2 = "Geek"
var3 = var1 + var2
print (var3)
|
String Concatenation using the join() Method in Python
The join() method is a string method and returns a string in which the elements of the sequence have been joined by a string separator. This method combines the string that is stored in var1 and var2. It accepts only the list as its argument and list size can be anything.
Python3
var1 = "Geeks"
var2 = "forGeeks"
print ("".join([var1, var2]))
var3 = " " .join([var1, var2])
print (var3)
|
Output
GeeksforGeeks
Geeks forGeeks
The time complexity of both methods is O(n), where n is the total length of the strings.
The auxiliary space complexity for the second join() method is also O(n), as it creates a new string that contains the combined string along with the separator.
Concatenate Strings in Python using ‘%’ Operator
We can use the % operator for string formatting, it can also be used for string concatenation. It’s useful when we want to concatenate strings and perform simple formatting. The %s denotes string data type. The value in both the variable is passed to the string %s and becomes “Hello World”.
Python3
var1 = "Welcome"
var2 = "Geek"
print ( "% s % s" % (var1, var2))
|
Python String Concatenation using format() function in Python
The str.format() is one of the string formatting methods in Python, which allows multiple substitutions and value formatting. It concatenates elements within a string through positional formatting. The curly braces {} are used to set the position of strings. The first variable stores in the first curly braces and the second variable stores in the second curly braces. Finally, it prints the value “Hello World”.
Python3
var1 = "Hello"
var2 = "Geeks"
print ( "{} {}" . format (var1, var2))
var3 = "{} {}" . format (var1, var2)
print (var3)
|
Output
Hello Geeks
Hello Geeks
Concatenate Strings in Python using “, ” comma
A comma “,” is a great alternative to string concatenation using “+”. when you want to include single whitespace. Use a comma when you want to combine data types with single whitespace in between.
Python3
var1 = "Geeks"
var2 = "for"
var3 = "Geeks"
print (var1, var2, var3)
|
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
27 Jul, 2023
Like Article
Save Article