How do you concatenate a variable and a string 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


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?

How do you concatenate a variable and a string in python?

asked Aug 21, 2013 at 4:09

How do you concatenate a variable and a string 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       ^

How do you concatenate a variable and a string in python?

answered Aug 21, 2013 at 4:10

How do you concatenate a variable and a string in python?

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

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

How do you concatenate a variable and a string 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))

How do you concatenate a variable and a string in python?

answered Jun 24, 2016 at 3:17

How do you concatenate a variable and a string 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.

How do you concatenate a variable and a string 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]

How do you concatenate a variable and a string 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]}"

How do you concatenate a variable and a string in python?

answered Jul 24, 2019 at 2:34

DoryxDoryx

3674 silver badges12 bronze badges

2

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

Python Concatenate String and int.
Using str() function. The easiest way is to convert int to a string using str() function. ... .
Using % Operator. print("%s%s" % (s, y)).
Using format() function. We can use string format() function too for concatenation of string and int. ... .
Using f-strings..

How do you combine variables and strings?

In JavaScript, we can assign strings to a variable and use concatenation to combine the variable to another string. To concatenate a string, you add a plus sign+ between the strings or string variables you want to connect. let myPet = 'seahorse'; console.

How do you concatenate two things in Python?

Using '+' operator Two strings can be concatenated in Python by simply using the '+' operator between them. More than two strings can be concatenated using '+' operator.

What is variable with string concatenation?

To use the Variable with String Concatenation Pattern a variable is defined which stores the value of a string combined with another string or number.