Start New Discussion within our Software Development Community

I have code that when executed will give me a long list output, which is fine, but how can I get it to print without the parenthesis and commas? example: answer =('7', 'Q', '5', 'H', '9', '4', '3', '8', 'L') but I'd like it to print like this: 7Q5H9438L I tried ans=answer.split(",") to remove commas but it didn't work.

>>> answer =('7', 'Q', '5', 'H', '9', '4', '3', '8', 'L')
>>> ''.join(answer)
answer = ('7', 'Q', '5', 'H', '9', '4', '3', '8', 'L')
ans = ''.join(answer)
print ans

Should give you 7Q5H9438L . Did this help?

what does join do? does it replace only commas or any other joining charcter?
I'll dig py docs but forum sometimes is best place for answers

join takes every element of an iterable container (must contain strings/chars) and "joins" them together using the first argument as the joining element.


>>> a = '12345'
>>> '__'.join(a)
>>> b=('1','2','3','4','5')
>>> '|!|'.join(b)
>>> c = ['1','2','3','4','5']
>>> import string
>>> string.join( c, '(*)' )
>>> d = {1:'a', 2:'b', 3:'c'}
>>> ','.join(d.values())

As you can see, join(list [,sep]) is analgous to sep.join(list) . Here's the official description of the second way:

S.join(sequence) -> string

Return a string which is the concatenation of the strings in the
sequence. The separator between elements is S.

answer = ('7', 'Q', '5', 'H', '9', '4', '3', '8', 'L')
ans = ''.join(answer)
print ans

Should give you 7Q5H9438L . Did this help?

Yes :) just what I was looking for, thank you

I have a similar problem but what I want to do is to remove the commas but keep the spacing inbetween them. Is that possible?

Please start a new thread for your question instead. If the file is not too long you can just read it in and use the replace method of strings to replace comma with nothing. That would not allow any commas inside the data.

This question has already been answered. Start a new discussion instead.