Python String Concatenation

In Python, it is possible to concatenate two different strings, even with the same string to itself multiple times using the + and the * operator. Read this article to learn more about Python String Concatenation.

What is Python String Concatenation?

The + operator is an addition operator and will return the sum of two numbers. Hence, the + symbol will act as a string concatenation operator in Python. Thus, it will function with two string operands and get the end output as the concatenation of the two.

Hence, the characters of the string on the right of the plus symbol will appear at the left of its string. The result of the concatenation will be considered as the new string.

What are the different ways to concatenate strings?

Python String Concatenation will be considered as the method of adding two strings. Check out the different methods to concatenate strings below:

  1. Using + operator
  2. Using join() method
  3. Using % operator
  4. Using format() function
  5. Using “,” (comma)
  6. Using f-string (Literal String Interpolation)

1. String Concatenation using ‘+’ Operator

In Python, when you join two strings using the + sign, it’s called string concatenation. It’s like combining two pieces of text. However, you need to remember that strings, which are sequences of characters, cannot be changed directly. Whereas, if this operator is used on integers, then it will perform the mathematical addition.

Example 1

In this example, var1 and var2 will be stored in another variable such as var3

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

Output

Hello Skillvertex

2. String Concatenation using join() Method

The join() Method will be considered a String Method. Thus, will return the string where the elements of the sequence will be joined by the string operator.

Therefore, it will accept only the list as its argument, and the list size will be anything. The example below illustrates the Python concatenate string with the join() operator.

var1 = "Skill"
var2 = "vertex"
 
# 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

Skillvertex
Skillvertex

3. String Concatenation using ‘%’ Operator

The % Operator will work for string formatting. Additionally, it will be used as a string concatenation. This operator is beneficial when you need to concatenate strings and will do simple formatting.

Example

var1 = "Welcome"
var2 = "Skillvertex"
 
# % Operator is used here to combine the string
print("% s % s" % (var1, var2))

Output

Welcome Skillvertex

4. String Concatenation using the format() function

The str, format will be considered as the string formatting method where they provide multiple substitutions and value formatting. Hence, it can concatenate the elements within the string through positional formatting.

Example

var1 = "Hello"
var2 = "Skillvertex"
 
# 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 Skillvertex
Hello Skillvertex

5. String Concatenation using “, ” comma

A comma will be considered as a great alternative to the string concatenation with the + operator. Thus, it is required to use the comma for combining the data types with the single whitespace in between.

var1 = "Skill"
var2 = "Vertex"
var3 = ".com"
 
# using comma to combine data types
# with a single whitespace.
print(var1, var2, var3)

Output

Skillvertex.com

6. String Concatenation using f-string

Using the F-string for the string concatenation will be operated on the Python versions above 3.6 and will be introduced in the PEP 498- Literal String Interpolation.

# Variables
var1 = "Skill"
var2 = "Vertex"
var3 = ".com"

# String concatenation using f-string
result = f"{var1} {var2} {var3}"

# Printing the result
print(result)

Output

Skill Vertex .com

Conclusion

In Python, string concatenation is the process of combining multiple strings into a single string. The + operator is commonly used for this purpose. When concatenating strings, it’s important to remember that strings are immutable, meaning the original strings are not modified; instead, a new string is created.

Additionally, Python offers convenient f-strings (formatted string literals) for concise and readable string concatenation, allowing variables to be easily inserted into strings. This straightforward process of joining strings provides flexibility in constructing meaningful and dynamic text outputs in Python programs.

Python String Concatenation- FAQs

Q1.What symbol is used to concatenate 2 strings in Python?

Ans. The + operator will combine the 2 strings.

Q2. What functions concatenate two strings?

Ans. In C, the strcat() function will operate to concatenate on the two strings.

Q3.How do I concatenate two string columns in Python?

Ans.df[“full_name”] = df[“first_name”] + “+df[“last_name”]
df[“full_name”] = df[[“first_name”,”last_name”]].agg(” “.join, axis=1)
df = df.withColumn( “full_name”, F.concat(“first_name”, F.lit(” “), “last_name”) )

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

Leave a Comment