1.11M Members

removing commas from list

 
0
 

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.

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

Should give you 7Q5H9438L . Did this help?

 
0
 

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

 
0
 

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.

Question Answered as of 5 Years Ago by jlm699, Stefano Mtangoo and crono5788
 
0
 
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

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article