0
Can split method of list be used to separate each alphabet of a word in the list? Or an alternate way to do it?
Hi, if I am writing a code where a user is to input a string, I can easily convert it into a list of separate words of that string by using split method. But what if I wanted to spilt the alphabets too? Is there a way to do it? Simply, what I want is to have user input a word (let's just say only one word) and have it's alphabets in a list separately. I don't have much creativity, so I learn only this way. Thanks for all the help in advance
10 odpowiedzi
+ 4
list() makes a list out of every character in a string or similar consecutive character occurence, even the spaces end up there.
.split() literally splits a string or whatever into different pieces. it splits spaces if no parameter is given.
+ 6
Another way
a = "my string"
b = [ *a ] #See tuple unpacking
You can imagine strings as tuples of characters. The methods shared by Slick, Kiibo Ghayal and myself are equivalent in behavior. I think Slick's one is better because it's easier to read.
Oma Falk Thanks.
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 What is separator? I don't know much of Python terminology.
+ 5
a = 'this is a string'
b = list(a)
print(b)
+ 2
Thanks for the answer, it's working but can you please tell me how this worked? The way I know it, list should have only put the brackets around the string and define a list in code. How did it split the alphabets?
+ 2
Kevin ★ that's very nice
+ 1
I see it now, thanks for explaining.
- 1
Hi