Reputation: 21
How to convert a list of numbers to a refromatted string.
I am new to the list and have seen simple Python type programming that can be executed in a simple text editor.
I have a List of numbers and would like to convert to list to a string of number with some additions.
i.e.
numbers
1234
1235
1236
1237
change to:
"1234.ARD" "1235.ARD" "1236.ARD" "1237.ARD"
If this is at all possible please let me know.
Thanks in advance.
Upvotes: 2
Views: 152
Reputation:
What you want the output to be isn't exactly clear (is it a list of strings or a single string?).
Regardless, I'll give both solutions and you can pick whichever you want:
>>> lst = [1234, 1235, 1236, 1237]
>>> print (["{}.ARD".format(x) for x in lst]) # List of strings
['1234.ARD', '1235.ARD', '1236.ARD', '1237.ARD']
>>>
>>> print (" ".join(['"{}.ARD"'.format(x) for x in lst])) # Single string
"1234.ARD" "1235.ARD" "1236.ARD" "1237.ARD"
>>>
The important concepts here are:
Upvotes: 2
Reputation: 2605
Use string formatting and a list comprehension:
In [1]: numbers = [1234, 1235, 1236, 1237]
In [2]: ['%s.ARD' % n for n in numbers]
Out[2]: ['1234.ARD', '1235.ARD', '1236.ARD', '1237.ARD']
Upvotes: 3