1,105,406 Community Members

removing commas from list

Member Avatar
kiddo39
Junior Poster in Training
50 posts since Nov 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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.

Member Avatar
crono5788
Newbie Poster
6 posts since Nov 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 
answer = ('7', 'Q', '5', 'H', '9', '4', '3', '8', 'L')
ans = ''.join(answer)
print ans

Should give you 7Q5H9438L . Did this help?

Member Avatar
jlm699
Veteran Poster
1,104 posts since Jul 2008
Reputation Points: 320 [?]
Q&As Helped to Solve: 295 [?]
Skill Endorsements: 0 [?]
 
0
 
>>> answer =('7', 'Q', '5', 'H', '9', '4', '3', '8', 'L')
>>> ''.join(answer)
'7Q5H9438L'
>>>
Member Avatar
Stefano Mtangoo
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 441 [?]
Q&As Helped to Solve: 394 [?]
Skill Endorsements: 2 [?]
 
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

Member Avatar
jlm699
Veteran Poster
1,104 posts since Jul 2008
Reputation Points: 320 [?]
Q&As Helped to Solve: 295 [?]
Skill Endorsements: 0 [?]
 
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
Member Avatar
kiddo39
Junior Poster in Training
50 posts since Nov 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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