ealeon
ealeon

Reputation: 12452

Python Concatenating Strings

  1 def add(i):
  2     return '\''+i+'\''
  3 a = ['a', 'b']
  4 print " or ".join([add(i) for i in a])

OUTPUT: 'a' or 'b'

I am not sure if the above is the best way (esp the add function).
Is there a better way to achieve what I am trying to do?

Upvotes: 1

Views: 73

Answers (1)

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 250871

You can use repr:

>>> a = ['a', 'b']
>>> print " or ".join(repr(i) for i in a)
'a' or 'b'

Upvotes: 5

Related Questions