We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,809 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

removing commas from list

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.

4
Contributors
5
Replies
5 Hours
Discussion Span
4 Years Ago
Last Updated
7
Views
Question
Answered
kiddo39
Junior Poster in Training
50 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
answer = ('7', 'Q', '5', 'H', '9', '4', '3', '8', 'L')
ans = ''.join(answer)
print ans

Should give you 7Q5H9438L . Did this help?

crono5788
Newbie Poster
6 posts since Nov 2007
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0
>>> answer =('7', 'Q', '5', 'H', '9', '4', '3', '8', 'L')
>>> ''.join(answer)
'7Q5H9438L'
>>>
jlm699
Veteran Poster
1,112 posts since Jul 2008
Reputation Points: 355
Solved Threads: 293
Skill Endorsements: 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

Stefano Mtangoo
Senior Poster
3,731 posts since Jun 2007
Reputation Points: 462
Solved Threads: 396
Skill Endorsements: 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.

jlm699
Veteran Poster
1,112 posts since Jul 2008
Reputation Points: 355
Solved Threads: 293
Skill Endorsements: 0
Question Answered as of 4 Years Ago by jlm699, Stefano Mtangoo and crono5788
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

kiddo39
Junior Poster in Training
50 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0722 seconds using 2.65MB