This is my code:
list = []
text = input("Please give your text:")
text = str(text)
list.append(text)
list = str(list)
And I want to give the input, for example: abcd. Then, I want it to split it in ["a","b","c","d"]
This is my code:
list = []
text = input("Please give your text:")
text = str(text)
list.append(text)
list = str(list)
And I want to give the input, for example: abcd. Then, I want it to split it in ["a","b","c","d"]
Another option:
text = input("Please give your text:")
l = list(text)
print (l)
First, avoid naming your variable as list
. You can then, split a str
into list just by saying list(text)
.
lst = []
text = input("Please give your text:")
lst.extend(list(text))
print (lst)
Output
['a', 'b', 'c', 'd']
Access the characters by Index. By iterating over the string, you can pull out each character individually. The variable i
will be equal to index 0
to the length of text
and you can access the character elements by index with text[i]
. See example below:
user_input_as_chars = []
text = input("Please give your text:")
for i in range(0, len(text)):
user_input_as_chars.append(text[i])
user_input_as_chars = str(list)
Another approach is to traverse the Strings as an iterable:
for char in text:
user_input_as_chars.append(char)