Python Forum

Full Version: String coming up weird
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello there,
I have not used Python in a while - I've been focusing on JavaScript - so this may be one of those 'noob questions' people refer to. I made a variable of strings like this:
cmd = 'sudo ./pi_fm_rds -freq 93 -audio', file.name, '-ps SATSUN -rt SATSUNRADIO'
then I do:
print(cmd)
but it comes out as this:
('sudo ./pi_fm_rds -freq 93 -audio', '/home/pi/Desktop/test.py', '-ps SATSUN -rt SATSUNRADIO')
instead of one big string.
Does anyone know why it does this? How can I fix this?
Thanks in advance for any help,
Eddie
Edit: file.name is defined in the code
to answer your question - the way you define cmd makes it a tuple, not string. Brackets are not required, as stated in the docs:
Quote:They may be input with or without surrounding parentheses, although often parentheses are necessary anyway (if the tuple is part of a larger expression).
So when you print it - you get tuple (on output tuples are always enclosed in parentheses, so that nested tuples are interpreted correctly)
There are different ways to construct the string, however, if you want to execute command in shell/terminal - look at subprocess.run(). Don't construct string, but pass command and arguments as list.