#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    1
    Rep Power
    0

    new to python, question regarding string


    Hi,

    Well... I was trying to convert string into list of characters.

    My job is to take the string and convert it to lowercase and sorted alphabetical order.
    For example, if string "hash" is given, then I have to convert it into "ahhs"

    So, I was looking for method something like "toArray" in Java which takes String and convert it into array list of characters, but I couldnt find one.

    Is there anyway I can do this process without using any loops?
    Please any help?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    You can use list(x) to convert any sequence to a list, or tuple(x) to turn a sequence to a tuple. Since a string is a sequence of characters, it will do what you want.

    To do the reverse and convert a list of characters back into a string you need to use the string join method.

    Code:
    >>> s = "Hello World"
    >>> s = s.lower()
    >>> l = list(s)
    >>> l.sort()
    >>> print l
    [' ', 'd', 'e', 'h', 'l', 'l', 'l', 'o', 'o', 'r', 'w']
    >>> ''.join(l)
    ' dehllloorw'
    >>>
    Dave - The Developers' Coach

IMN logo majestic logo threadwatch logo seochat tools logo