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.

Recommended Answers

All 7 Replies

>>> answer =('7', 'Q', '5', 'H', '9', '4', '3', '8', 'L')
>>> ''.join(answer)
'7Q5H9438L'
>>>
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.

Examples:

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

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

join(...)
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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.