A string is actually a list of characters. Each element in the string can be accessed with
brackets. Yet, if we try to modify a single character, we would fail as Python does not allow that. An error will occur telling
TypeError: 'str' object does not support item assignment.
We can actually turn a string into a list, where each letter is a separate string in that list. This can be accomplished with
list(). Just like we’ve turned integers to strings with
str(), or strings into floats with
float(), we can similarly turn a string into a list.
fruit = 'apple' l = list(fruit) print(fruit) # apple print(l) # ['a', 'p', 'p', 'l', 'e'] l = '@' print(l) # ['a', '@', 'p', 'l', 'e']
Here, the string
fruitwas turned into a list
l, which was later modified to contain different characters.
Given 5 strings, your task is to print the list of the characters of all the 5 strings (in the order of the input).
The input contains 5 lines - each one having a single string.
The program should print a list of all the characters in those 5 strings.
apple banana cherry orange pineapple
['a', 'p', 'p', 'l', 'e', 'b', 'a', 'n', 'a', 'n', 'a', 'c', 'h', 'e', 'r', 'r', 'y', 'o', 'r', 'a', 'n', 'g', 'e', 'p', 'i', 'n', 'e', 'a', 'p', 'p', 'l', 'e']