I'm trying to print the length of this list, but whenever I'm trying to print the output is 1. The compiler is treating whole list as single element. So how can I get length of this list?
import random
names_string = input("Give me everybody's names, separated by a comma. ")
names = names_string.split(", ")
print(len(names))
This displays
Give me everybody's names, separated by a comma. Yo,Gh,Kl
1
CodePudding user response:
Your input text is separated by commas with no spaces. However, your code splits on ', ', a comma followed by a space. Whenever you split on a string which doesn't occur in the input string, the result is a list containing just the input string.
Therefore, you should do names_string.split(',').
CodePudding user response:
Your problem is that you are splitting the names_string by , which if you look carefully includes a comma AND a space, but when you input your data "Yo,Gh,Kl" you didn't include spaces after the commas. When split() was called, no instances of , were found.
