I have a list of counters
counters = ['76195087', '963301809', '830123644', '60989448', '0', '0', '76195087', '4006066839', '390361581', '101817210', '0', '0']
and I would like to create a string using some of these counters....
cmd = 'my_command' + counters[0:1]
But I find that I am unable to concatenate strings and lists.
What I must have at the end is a string that looks like this:
my_command 76195087
How do I get these numbers out of their list and get them to behave like strings?
You can join strings in a list with, well, join:
cmd = 'my_command' + ''.join(counters[:1])
But you shouldn't construct a command like that in the first place and give it to os.popen or os.system. Instead, use the subprocess module, which handles the internals (and escapes problematic values):
import subprocess
# You may want to set some options in the following line ...
p = subprocess.Popen(['my_command'] + counters[:1])
p.communicate()
If you just want a single element of the list, just index that element:
cmd = 'my_command ' + counters[0]
If you want to join several elements, use the 'join()' method of strings:
cmd = 'my_command ' + " ".join(counters[0:2]) # add spaces between elements
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With