How to use python variable in os.system?

后端 未结 2 547
醉酒成梦
醉酒成梦 2021-01-19 12:00

I am creating small console script in python, and I will like to put cowsay command in it, but cow says name of the variable, where the string is, not the string inside the

2条回答
  •  -上瘾入骨i
    2021-01-19 12:57

    You could use format to construct the string

    os.system('cowsay {}'.format(word))
    

    Or simple string concatenation

    os.system('cowsay ' + word)
    

    But I prefer the former, especially if the string get more complicated.

提交回复
热议问题