|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
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 |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > new to python, question regarding string |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|