[Tutor] elegant way to turn list into string?

tanja pislar tanja.pislar at gmail.com
Sat May 7 10:52:26 CEST 2005


hi Max,
you can use:

alph = ["a","b", ....]
myString = "".join(alph)
print myString


regards,
tanja

On 5/7/05, Max Russell <max_russell2000 at yahoo.co.uk> wrote:
> Hello-
> 
> I've started writing a suite of tools to help me do
> Crypto puzzles (slowly...) My first snippet of code
> allos me to print out a Caesar shifting alphabet, as I
> tend to write them out by hand; which is a waste of
> time.
> 
> alph =
> ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
> print "\n"
> print alph
> print "\n"
> x = 1
> while x < 26:
>     alph.append(alph.pop(0))
>     print alph
>     print "\n"
>     x += 1
> 
> anyway- I don't like the output which come in the
> form:
> ['b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
> 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
> 'w', 'x', 'y', 'z', 'a']
> 
> What I'd like to do is take my list and turn it into a
> string, which looks neater when printed.
> 
> I've tried using join() but that didn't work, can
> anyone give me a pointer here?
> 
> the other method I've used was dealing with a string
> directly:
> alph = 'abcdefghijklmnopqrstuvwxyz'
> print alph
> #print "\n"
> 
> x = 1
> 
> while x < 26:
>     new_alph = alph[1:] + alph[:1]
>     print new_alph
>     print "\n"
>     x += 1
> 
> But this has the drawback of not progressing with my
> newly create alphabet, it just returns:
> abcdefghijklmnopqrstuvwxyz
> bcdefghijklmnopqrstuvwxyza
> 
> bcdefghijklmnopqrstuvwxyza
> 
> etc...
> 
> HELP!!
> 
> thanks
> Max
> 
> 
> ___________________________________________________________
> Yahoo! Messenger - want a free and easy way to contact your friends online? http://uk.messenger.yahoo.com
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


-- 
www.klaustrofobik.org


More information about the Tutor mailing list