0
Any way to convert a string to a list where each character of the string is a separate item on the list? Using Python.
I'd like to do something along the lines of this: String = "this is a string" and after conversion the list would look like this: list = [t,h,i,s,i,s,a,s,t,r,i,n,g]
5 Respostas
+ 2
Brendan Goodwin
Please keep this in mind a string in python is a list of characters.
a = "thisisastring"
print(a[1])
result: h
+ 2
s = "This is a string"
l = []
for x in s:
if x.isalpha():
l.append(x)
print(l)
+ 1
String = "this is a string"
list = [i for i in String]
print(list)
#o/p: It will give list of all each character of the string into a separate list
0
string="this is a string"
l=list(string)
print(l)
output= list of all the characters
0
i think using the split function, it will help you.
string = " this is a string"
A = []
for i in string.split():
A.append (i)
print A
the problem about the split function is that,it will only split the the string if there is a space or a punctuation mark.
that's what I have