Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
String coming up weird
#1
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
Reply
#2
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to know coming snmp trap version in python pysnmp? ilknurg 0 2,561 Jan-31-2022, 12:16 PM
Last Post: ilknurg
  const or #define coming from C jamie_01 11 3,292 Sep-16-2021, 05:39 PM
Last Post: jefsummers
  Weird SQLAchemy connection string error pawpaw 0 1,493 Jun-28-2020, 10:11 AM
Last Post: pawpaw
  Timer keeps coming up with a non-callable int object birdwatcher 1 3,130 Feb-16-2020, 12:40 PM
Last Post: DeaD_EyE
  Error Message Coming Up When Running Code eddywinch82 0 1,854 Feb-10-2020, 11:48 PM
Last Post: eddywinch82
  need help coming up with a code Staph 0 1,439 Jun-21-2019, 10:06 AM
Last Post: Staph
  Learning python My lists are not coming out right TheMusicMan 3 2,559 Aug-17-2018, 02:45 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020