How do you concatenate variables 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


When you're learning a programming language, you're likely to come across a data type called a string. A string usually contains a series of characters nested in quotation marks that can be represented as a text.

In this article, we will talk about string concatenation in Python. This is the process of joining/adding one string to another. For example, the concatenation of "freeCode" and "Camp" is "freeCodeCamp".

String concatenation is important when working with two or more separate variables (strings) that are combined to form a much larger string.

This also enables you have separate units of a string even after combining them, just in case you need to use one string variable somewhere else in your code and not the entire string.

To concatenate string in Python, we use the + operator to append the strings to each other. Here is an example:

x = "Happy"
y = "Coding"
z = x + y
print(z) 
#HappyCoding

In the code above, we created two variables (x and y) both containing strings – "Happy" and "Coding" – and a third variable (z) which combines the two variables we created initially.

We were able to combine the two variables by using the + operator. Our output after this was HappyCoding. If you were to reverse the order during concatenation by doing this: z = y + x then we would get CodingHappy printed to the console.

ADVERTISEMENT

How to Add Spaces Between Concatenated Strings

You might notice that there was no space between the variables when printed. Here's how we can add spaces between concatenated strings:

x = "Happy"
y = "Coding"
z = x + " " + y
print(z) 
# Happy Coding

You'll notice that there is a space between the quotation marks. If you omit the space then the strings will still be closely joined together.

You can also add spaces at the end of a string when it is created and it will be applied when printed. Here's how you'd do that:

x = "Happy "
y = "Coding"
z = x + y
print(z) 
#Happy Coding

ADVERTISEMENT

How to Create Multiple Copies of a String Using the * Operator

When we use the * operator on a string, depending on value passed, we create and concatenate (append) copies of the string. Here is an example:

x = "Happy"
y = x * 3
print(y) 
# HappyHappyHappy

Using the * operator, we duplicated the value of the string "Happy" three times with the three printed values closely packed together.

If we were to add a space at the end of the string, then the strings will be separated. That is:

x = "Happy "
y = x * 3
print(y) 
# Happy Happy Happy

ADVERTISEMENT

Conclusion

In this article, we learned how to combine strings in Python through concatenation.

Thank you for reading and happy coding!

Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started

Summary: in this tutorial, you’ll learn various ways to concatenate strings in Python.

Python provides you with various ways to concatenate one or more strings into a new string.

Since Python string is immutable, the concatenation always results in a new string.

1) Concatenating literal strings

To concatenate two or more literal strings, you just need to place them next to each other. For example:

s = 'String' ' Concatenation' print(s)

Code language: PHP (php)

Output:

String Concatenation

Code language: JavaScript (javascript)

Note that this way won’t work for the string variables.

2) Concatenating strings using the + operator

A straightforward way to concatenate multiple strings into one is to use the + operator:

s = 'String' + ' Concatenation' print(s)

Code language: PHP (php)

And the + operator works for both literal strings and string variables. For example:

s1 = 'String' s2 = s1 + ' Concatenation' print(s2)

Code language: PHP (php)

Output:

String Concatenation

Code language: JavaScript (javascript)

3) Concatenating strings using the += operator

Similar to the + operator, you can use the += operator to concatenate multiple strings into one:

s = 'String' s += ' Concatenation' print(s)

Code language: PHP (php)

Output:

String Concatenation

Code language: JavaScript (javascript)

4) Concatenating strings using the join() method

The join() method allows you to concatenate a list of strings into a single string:

s1 = 'String' s2 = 'Concatenation' s3 = ''.join([s1, s2]) print(s3)

Code language: PHP (php)

Output:

StringConcatenation

The join() method also allows you to specify a delimiter when concatenating strings. For example:

s1 = 'String' s2 = 'Concatenation' s3 = ' '.join([s1, s2]) print(s3)

Code language: PHP (php)

Output:

String Concatenation

Code language: JavaScript (javascript)

In this example, we use the join() method to concatenate strings delimited by a space.

The following example use the join() method to concatenate strings delimited by a comma:

s1, s2, s3 = 'Python', 'String', 'Concatenation' s = ','.join([s1, s2, s3]) print(s)

Code language: PHP (php)

Output:

Python,String,Concatenation

Code language: JavaScript (javascript)

5) Concatenating strings using the %-formatting

String objects have the built-in % operator that allows you to format strings. Also, you can use it to concatenate strings. For example:

s1, s2, s3 = 'Python', 'String', 'Concatenation' s = '%s %s %s' % (s1, s2, s3) print(s)

Code language: PHP (php)

Output:

Python String Concatenation

Code language: JavaScript (javascript)

In this example, Python substitutes a %s in the literal string by corresponding string variable in the tuple that follows the % operator.

6) Concatenating strings using the format() method

You can use the format() method to concatenate multiple strings into a string. For example:

s1, s2, s3 = 'Python', 'String', 'Concatenation' s = '{} {} {}'.format(s1, s2, s3) print(s)

Code language: PHP (php)

Output:

Python String Concatenation

Code language: JavaScript (javascript)

In this example, you use the {} in the string literal and pass the string that you want to concatenate to the format() method. The format() method substitutes the {} by the corresponding string argument.

7) Concatenating strings using f-strings

Python 3.6 introduced the f-strings that allow you to format strings in a more concise and elegant way.

And you can use the f-strings to concatenate multiple strings into one. For example:

s1, s2, s3 = 'Python', 'String', 'Concatenation' s = f'{s1} {s2} {s3}' print(s)

Code language: PHP (php)

Output:

Python String Concatenation

Code language: JavaScript (javascript)

Which method should you use to concatenate strings

Even though there’re multiple ways to concatenate strings in Python, it’s recommended to use the join() method, the + operator, and f-strings to concatenate strings.

Did you find this tutorial helpful ?

How do you concatenate text and variables in Python?

We can perform string concatenation using following ways: Using + operator. Using join() method. Using % operator. Using format() function.

How do you concatenate variable names in Python?

Combining Variables in Python. You can also combine variables in a print statement using the plus sign ( + ) operator. This operator will work as long as both values are of the same data type.

What is concatenation in Python with example?

Concatenating means obtaining a new string that contains both of the original strings. In Python, there are a few ways to concatenate or combine strings. The new string that is created is referred to as a string object. In order to merge two strings into a single object, you may use the + operator.

How do you concatenate 3 strings in Python?

Concatenation means joining strings together end-to-end to create a new string. To concatenate strings, we use the + operator. Keep in mind that when we work with numbers, + will be an operator for addition, but when used with strings it is a joining operator.