how would I add set elements to a string in python? I tried:
sett = set(['1', '0'])
elements = ''
for i in sett:
elements.join(i)
but no dice. when I print elements the string is empty. help
how would I add set elements to a string in python? I tried:
sett = set(['1', '0'])
elements = ''
for i in sett:
elements.join(i)
but no dice. when I print elements the string is empty. help
elements.join(i) does not change elements. You need to assign the value returned by join to something:
s = set(['1', '0'])
elements = ''
for i in s:
elements = elements.join(i)
But, as others pointed out, this is better still:
s = set(['1', '0'])
elements = ''
elements = elements.join(s)
or in its most concise form:
s = set(['1', '0'])
elements = ''.join(s)
This should work:
sett = set(['1', '0'])
elements = ''
for i in sett:
elements += i
# elements = '10'
However, if you're just looking to get a string representation of each element, you can simply do this:
elements = ''.join(sett)
# elements = '10'
''.join(sett) is simpler and more efficient.Don't know what you mean with "add set elements" to a string. But anyway: Strings are immutable in Python, so you cannot add anything to them.