Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use concatenate a fixed string and a variable in Python

I want to include file name 'main.txt' in the subject for that I am passing file name from command line. but getting error in doing so

python sample.py main.txt #running python with argument   msg['Subject'] = "Auto Hella Restart Report "sys.argv[1]  #line where i am using that passed argument 
like image 575
Shivam Agrawal Avatar asked Aug 21 '13 04:08

Shivam Agrawal


People also ask

How do you concatenate the string and variable in print statement in Python?

Python Print Variable - String Concatenation: language = "Python" #Adding the language variable into a string print("Hello" +" "+ language +" "+ "World!") #Output - “Hello Python World!” While using this method, spaces are not added and you would have to manually add them and hence this method is not used very often.

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.


2 Answers

I'm guessing that you meant to do this:

msg['Subject'] = "Auto Hella Restart Report " + sys.argv[1] # To concatenate strings in python, use       ^  
like image 174
Brionius Avatar answered Sep 19 '22 01:09

Brionius


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

for integer type ...

variable="  10"   print (variable)   print("This is the Test File "+str(variable))   
like image 42
Smith John Avatar answered Sep 20 '22 01:09

Smith John