Python Forum

Full Version: String add prefix & suffix
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
How can I do this in Python ? newbie here

1,2,3 to tmp[1],tmp[2],tmp[3]
it can be 1 to tmp[1]

1,2 to tmp[1],tmp[2]
basically input can be 1 or 1,2 or 1,2,3 etc
nahom Wrote:How can I do this in Python ?
It is very difficult to understand what you want to do. Can you describe it with a few complete sentences?
Passing a string input and expecting output formatted.

for example :

i = 1,2,3
output should be
tmp[1],tmp[2],tmp[3]

I tried this, still suffix is not happening

>>> elements = ['elem1','elem2']
>>> output = ['tmp[{}'.format(i) for i, elem in enumerate(elements, 1)]
>>> output
['tmp[1', 'tmp[2']

I tried this, still suffix is not happening

>>> elements = ['1','2']
>>> output = ['tmp[{}'.format(i) for i, elem in enumerate(elements, 1)]
>>> output
['tmp[1', 'tmp[2']
Try this perhaps
output = ['tmp[{}]'.format(i) for i, e in enumerate(elements, 1)]