What is meant by string concatenation in python?

❮ Python Glossary


String Concatenation

String concatenation means add strings together.

Use the + character to add a variable to another variable:

Example

x = "Python is "
y = "awesome"
z =  x + y
print(z)

Try it Yourself »

Example

Merge variable a with variable b into variable c:

a = "Hello"
b = "World"
c = a + b
print(c)

Try it Yourself »

Example

To add a space between them, add a " ":

a = "Hello"
b = "World"
c = a + " " + b
print(c)

Try it Yourself »

For numbers, the + character works as a mathematical operator:

Example

x = 5
y = 10
print(x + y)

Try it Yourself »

If you try to combine a string and a number, Python will give you an error:

Example

x = 5
y = "John"
print(x + y)

Try it Yourself »



Python Variables Tutorial Creating Variables Variable Names Assign Value to Multiple Variables Output Variables Global Variables


❮ Python Glossary


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.

Example: 

Python3

var1 = "Welcome"

var2 = "statistics"

print(f"{var1} {var2}")

Output:

Welcome
 statistics

String Concatenation in Python

String Concatenation is the technique of combining two strings. String Concatenation can be done using many ways. 

  1. Using + operator 
  2. Using join() method 
  3. Using % operator 
  4. Using format() function 
  5. Using , (comma)

Method 1: String Concatenation using + Operator

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, whenever it is concatenated, it is assigned to a new variable.

Python3

var1 = "Hello "

var2 = "World"

var3 = var1 + var2

print(var3)

Output

Hello World

Method 2: String Concatenation using join() Method

The join() method is a string method and returns a string in which the elements of the sequence have been joined by str separator. This method combines the string that is stored in the var1 and var2. It accepts only the list as its argument and list size can be anything. 

Python3

var1 = "Hello"

var2 = "World"

print("".join([var1, var2]))

var3 = " ".join([var1, var2])

print(var3)

Output

HelloWorld
Hello World

Method 3: String Concatenation 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 = "Hello"

var2 = "World"

print("% s % s" % (var1, var2))

Output

Hello World

Method 4: String Concatenation using format() function 

str.format() is one of the string formatting methods in Python, which allows multiple substitutions and value formatting. It concatenate 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 = "World"

print("{} {}".format(var1, var2))

var3 = "{} {}".format(var1, var2)

print(var3)

Output

Hello World
Hello World

Method 5: String Concatenation using (, 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 = "Hello"

var2 = "World"

print(var1, var2)

Output

Hello World

What do you mean by string concatenation?

In formal language theory and computer programming, string concatenation is the operation of joining character strings end-to-end. For example, the concatenation of "snow" and "ball" is "snowball". In certain formalisations of concatenation theory, also called string theory, string concatenation is a primitive notion.

Can you concatenate strings in Python?

In Python, you can concatenate two different strings together and also the same string to itself multiple times using + and the * operator respectively.

How do you concatenate a string in a variable in Python?

String Concatenation can be done using many ways..
Using + operator..
Using join() method..
Using % operator..
Using format() function..
Using , (comma).

What is concatenation with example?

The concatenation of two or more numbers is the number formed by concatenating their numerals. For example, the concatenation of 1, 234, and 5678 is 12345678. The value of the result depends on the numeric base, which is typically understood from context.