how to use concatenate a fixed string and a variab

2019-01-24 00:44发布

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

4条回答
疯言疯语
2楼-- · 2019-01-24 00:49

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 begining

as

import sys

msg['Subject'] = "Auto Hella Restart Report " + sys.argv[1]
查看更多
Deceive 欺骗
3楼-- · 2019-01-24 00:53
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))  
查看更多
来,给爷笑一个
4楼-- · 2019-01-24 01:05

Just try

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

The + operator is overridden in python to concatenate strings.

查看更多
老娘就宠你
5楼-- · 2019-01-24 01:13

I'm guessing that you meant to do this:

msg['Subject'] = "Auto Hella Restart Report " + sys.argv[1]
# To concatenate strings in python, use       ^ 
查看更多
登录 后发表回答