Open In App
Related Articles

Python String Concatenation

Improve Article
Improve
Save Article
Save
Like Article
Like

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: 

  1. Using + operator 
  2. Using join() method 
  3. Using % operator 
  4. Using format() function 
  5. 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




# Defining strings
var1 = "Hello "
var2 = "Geek"
 
# + Operator is used to combine strings
var3 = var1 + var2
print(var3)


Output

Hello Geek

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"
 
# join() method is used to combine the strings
print("".join([var1, var2]))
 
# join() method is used here to combine
# the string with a separator Space(" ")
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"
 
# % Operator is used here to combine the string
print("% s % s" % (var1, var2))


Output

Welcome Geek

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"
 
# format function is used here to
# combine the string
print("{} {}".format(var1, var2))
 
# store the result in another variable
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"
 
# using comma to combine data types
# with a single whitespace.
print(var1, var2, var3)


Output

Geeks for Geeks

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
Similar Reads
Related Tutorials