+ 1

Is there any way to convert strings to lists?

Is there any way to convert strings to lists with each character of the string being one member of the list? Like a string "hello" could be converted into a list with list[0] = "h" and so on?

14th Oct 2019, 4:45 AM
Black Jack
10 odpowiedzi
+ 3
For the first part of your question, you could use the list() function: print(list('hello'))
14th Oct 2019, 4:49 AM
u007
u007 - avatar
+ 3
To convert the list to a string, you could use the join function: z = ''.join(y) print(z)
14th Oct 2019, 5:00 AM
u007
u007 - avatar
+ 2
And for the second part, you could use slicing: x = list('hello') y = x[::-1] print(y)
14th Oct 2019, 4:52 AM
u007
u007 - avatar
0
I actually need to reverse a string character by character.
14th Oct 2019, 4:45 AM
Black Jack
0
Thanks it worked. However could y be converted back to string? Coz i tried print(str(y)) but it gives a list, not a string
14th Oct 2019, 4:56 AM
Black Jack
0
Thanks!
14th Oct 2019, 5:00 AM
Black Jack
0
To be clear, note that those are two single quotes.
14th Oct 2019, 5:02 AM
u007
u007 - avatar
0
You’re welcome!
14th Oct 2019, 5:02 AM
u007
u007 - avatar
0
Yes got it working. Thanks for your time.
14th Oct 2019, 5:03 AM
Black Jack