+ 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?
10 Respostas
+ 3
For the first part of your question, you could use the list() function:
print(list('hello'))
+ 3
To convert the list to a string, you could use the join function:
z = ''.join(y)
print(z)
+ 2
And for the second part, you could use slicing:
x = list('hello')
y = x[::-1]
print(y)
0
I actually need to reverse a string character by character.
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
0
Thanks!
0
To be clear, note that those are two single quotes.
0
You’re welcome!
0
Yes got it working. Thanks for your time.