52

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Shivam Agrawal
  • 2,053
  • 4
  • 26
  • 42

6 Answers6

65

I'm guessing that you meant to do this:

msg['Subject'] = "Auto Hella Restart Report " + sys.argv[1]
# To concatenate strings in Python, use       ^
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Brionius
  • 13,858
  • 3
  • 38
  • 49
25

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

msg['Subject'] = f'Auto Hella Restart Report {sys.argv[1]}'
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Samwise Ganges
  • 395
  • 6
  • 11
  • 1
    Is this the recommended way over the `plus` concatenator? – Timo Nov 30 '20 at 10:05
  • 1
    Just what I'm looking for now! Thank you! – iconique Feb 03 '21 at 19:22
  • What is the name of the Python feature used here? [f-strings](https://en.wikipedia.org/wiki/Python_(programming_language)#Expressions)? – Peter Mortensen Mar 27 '22 at 16:52
  • 1
    This *seems* to repeat [Doryx's answers](https://stackoverflow.com/questions/18348717/how-to-concatenate-a-fixed-string-and-a-variable-in-python/57174458#57174458) (five months prior). What is the difference? Is there a real difference? (Single quotes vs. double quotes.) – Peter Mortensen Mar 27 '22 at 16:54
  • No difference in single or double quotes, most people like to use double quotes in strings so that if you have an apostrophe you don't have to escape it, `"I'm Doryx"` instead of `'I\'m Doryx'` – Doryx Mar 29 '22 at 13:16
12
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))
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Smith John
  • 125
  • 1
  • 3
7

Try:

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

The + operator is overridden in Python to concatenate strings.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
DotPi
  • 3,977
  • 6
  • 33
  • 53
5

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]
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Anto
  • 610
  • 5
  • 13
  • It must be `msg['Subject'] = 'your string' + sys.argv[1]` in the first example otherwise you get a syntax error. – Timo Nov 30 '20 at 10:05
4

With Python 3.6 and later:

msg['Subject'] = f"Auto Hella Restart Report {sys.argv[1]}"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Doryx
  • 377
  • 4
  • 12
  • What is the name of the Python feature used here? [f-strings](https://en.wikipedia.org/wiki/Python_(programming_language)#Expressions)? – Peter Mortensen Mar 27 '22 at 16:52
  • 1
    Yes! Also see here: https://peps.python.org/pep-0498/ for the terminology. Thanks for the edit by the way! – Doryx Mar 29 '22 at 13:14