Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
String add prefix & suffix
#1
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
Reply
#2
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?
Reply
#3
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']
Reply
#4
Try this perhaps
output = ['tmp[{}]'.format(i) for i, e in enumerate(elements, 1)]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  prefix ID Number with 0,00 make 3 digit. mg24 1 753 Oct-06-2022, 07:20 AM
Last Post: ibreeden
  add the suffix to the next line Paulman 2 1,409 Dec-05-2021, 10:48 AM
Last Post: Paulman
  prefix to infix rontoto 4 3,960 Dec-08-2019, 12:32 PM
Last Post: rontoto
  Best way of taking a date prefix from a line and forming a file path from it? Skaperen 5 3,009 Jul-22-2019, 11:50 PM
Last Post: Skaperen
  number plus suffix Skaperen 5 3,408 Jun-05-2019, 07:10 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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