Concatenate a variable 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.

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

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

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

I want to include a file name, 'main.txt', in the subject. For that I am passing a file name from the command line. But I get an error in doing so:

python sample.py main.txt # Running 'python' with an argument

msg['Subject'] = "Auto Hella Restart Report "sys.argv[1]  # Line where I am using that passed argument

How can I fix this problem?

Concatenate a variable in python

asked Aug 21, 2013 at 4:09

Concatenate a variable in python

Shivam AgrawalShivam Agrawal

1,9494 gold badges24 silver badges41 bronze badges

3

I'm guessing that you meant to do this:

msg['Subject'] = "Auto Hella Restart Report " + sys.argv[1]
# To concatenate strings in Python, use       ^

Concatenate a variable in python

answered Aug 21, 2013 at 4:10

Concatenate a variable in python

Using f-strings which were introduced in Python version 3.6:

msg['Subject'] = f'Auto Hella Restart Report {sys.argv[1]}'

Concatenate a variable in python

answered Dec 11, 2019 at 16:46

5

variable=" Hello..."
print (variable)
print("This is the Test File " + variable)

For an integer type:

variable = "  10"
print (variable)
print("This is the Test File " + str(variable))

Concatenate a variable in python

answered Jun 24, 2016 at 3:17

Concatenate a variable in python

Smith JohnSmith John

1351 silver badge3 bronze badges

1

Try:

msg['Subject'] = "Auto Hella Restart Report " + sys.argv[1]

The + operator is overridden in Python to concatenate strings.

Concatenate a variable in python

answered Aug 21, 2013 at 4:13

DotPiDotPi

3,7955 gold badges31 silver badges48 bronze badges

If you need to add two strings, you have to use the '+' operator.

Hence

msg['Subject'] = 'your string' + sys.argv[1]

And also you have to import sys in the beginning.

As

import sys

msg['Subject'] = "Auto Hella Restart Report " + sys.argv[1]

Concatenate a variable in python

answered Aug 21, 2013 at 4:16

AntoAnto

6005 silver badges13 bronze badges

1

With Python 3.6 and later:

msg['Subject'] = f"Auto Hella Restart Report {sys.argv[1]}"

Concatenate a variable in python

answered Jul 24, 2019 at 2:34

DoryxDoryx

3674 silver badges12 bronze badges

2

Is there a concatenate function in Python?

One of the most popular methods to concatenate two strings in Python (or more) is using the + operator. The + operator, when used with two strings, concatenates the strings together to form one.

How do you concatenate a number and a string in Python?

In Python, we normally perform string concatenation using the + operator. The + operator, however as we know, is also used to add integers or floating-point numbers..
Using str() ... .
Using format() ... .
Using '%' format specifier. ... .
Using f-strings. ... .
Printing the string using print().

What is concatenation in Python 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.