String concatenation from a list of string, using a praticle in front and one at the end for each element

  • A+
Category:Languages

I have an array of strings:

data = ['a', 'b', 'c', 'd'] 

I want to obtain:

s.a, s.b, s.c, s.d 

I tried:

"s., ".join(fields) 

Doesn't work because I need s. in front and , at the end

 


You were very close. Use a list comprehension for the operation you want to perform on every string, and then join the list of strings together:

data = ['a', 'b', 'c', 'd'] ', '.join(['s.'+x for x in data]) # 's.a, s.b, s.c, s.d' 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: