How can I convert a given string's individual characters into individual elements of a list in Python?
b = []
a = "golf"
for i in a:
print(i)
b.append(i)
I expect an output as:
b = ['g','o','l','f']
but the actual output is
g
o
l
f
How can I convert a given string's individual characters into individual elements of a list in Python?
b = []
a = "golf"
for i in a:
print(i)
b.append(i)
I expect an output as:
b = ['g','o','l','f']
but the actual output is
g
o
l
f
I expect an output as:
b = ['g','o','l','f']
That's what you'd get if you tried (after the loop):
print("b = {}".format(b))
but since there's no such thing in your code, there's no reason it should be printed...
but the actual output is
g
o
l
f
Well yes, that's what you asked for:
# ...
for i in a:
print(i) # <- HERE
# ...
Maybe you can try this:
b = []
a = "golf"
for i in a:
b.append(i)
b
The expected output will be:
['g','o','l','f']
Or:
b = []
a = "golf"
for i in a:
b.append(i)
print("b = {}".format(b))
and you will get this output:
b = ['g','o','l','f']