Beginner Python Help

For the discussion of Perl, Python, Ruby, and PHP and other interpreted languages.

Beginner Python Help

Post by lemiwinks on Wed Dec 08, 2010 2:20 pm
([msg=50296]see Beginner Python Help[/msg])

So I'm trying to print a list without the , format and in a specific range. I can find anything on this, at the very least i dont know what it would be called to search for. i want a list like:
words = [d, o, g, , c, a, t, , m, o, u, s, e]

to return
dog cat mouse stead of d, o, g, , c, a, t, , m, o, u, s, e
any help is appreciated
lemiwinks
New User
New User
 
Posts: 2
Joined: Wed Dec 08, 2010 2:14 pm
Blog: View Blog (0)


Re: Beginner Python Help

Post by Vulpine on Wed Dec 08, 2010 2:44 pm
([msg=50297]see Re: Beginner Python Help[/msg])

You can run through each item in your list and append it to a string. Throwing something together real quick, you might have...

Code: Select all
words = ['d', 'o', 'g', ' ', 'c', 'a', 't', ' ', 'm', 'o', 'u', 's', 'e']
string = ''

for i in words:
    string += i

print(string)


Which prints...

Code: Select all
'dog cat mouse'
User avatar
Vulpine
Poster
Poster
 
Posts: 379
Joined: Fri Mar 26, 2010 11:14 pm
Blog: View Blog (0)


Re: Beginner Python Help

Post by fabianhjr on Wed Dec 08, 2010 3:38 pm
([msg=50299]see Re: Beginner Python Help[/msg])

Actually it is easier just to
Code: Select all
''.join(list)
Donate bitcoins to me! [1DhRP3hHgmSLQdRTZyT8VPTmzAj7Z2rsGA]
Dunno what bitcoins are? BitcoinMe
fabianhjr
Poster
Poster
 
Posts: 286
Joined: Tue Sep 21, 2010 7:48 pm
Blog: View Blog (0)


Re: Beginner Python Help

Post by Defience on Wed Dec 08, 2010 3:53 pm
([msg=50300]see Re: Beginner Python Help[/msg])

fabianhjr wrote:Actually it is easier just to
Code: Select all
''.join(list)


Indeed......

Code: Select all
>>> words=['f','i','r','e','a','r','m','s',' ','a','r','e',' ','f','o','r',' ','f','u','n']
>>> ''.join(words)
'firearms are for fun'
User avatar
Defience
Addict
Addict
 
Posts: 1280
Joined: Thu Jun 12, 2008 3:16 pm
Blog: View Blog (0)


Re: Beginner Python Help

Post by Vulpine on Wed Dec 08, 2010 3:58 pm
([msg=50301]see Re: Beginner Python Help[/msg])

Nice. I didn't know that one. Thank you, gentlemen.
User avatar
Vulpine
Poster
Poster
 
Posts: 379
Joined: Fri Mar 26, 2010 11:14 pm
Blog: View Blog (0)


Re: Beginner Python Help

Post by lemiwinks on Thu Dec 09, 2010 10:41 am
([msg=50335]see Re: Beginner Python Help[/msg])

Perfect, thanks
lemiwinks
New User
New User
 
Posts: 2
Joined: Wed Dec 08, 2010 2:14 pm
Blog: View Blog (0)



Return to Interpreted Languages

Who is online

Users browsing this forum: No registered users and 0 guests