Search code examples
pythonstring-concatenation

How to concatenate a fixed string and a variable in Python


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?


Solution

  • I'm guessing that you meant to do this:

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