冉冉说
isspace()如果字符串中的所有字符都是空格,则该方法返回 True,否则返回 False。当您c.isspace() for c in text逐个字符执行此操作时,如果它是空格,则返回 True(1),否则返回 False(0)然后sum按照提示操作,将 True(1)和 False(0)相加。您可以添加更多的打印语句以获得更深入的理解。text = input("Enter Text: ")spaces = sum(c.isspace() for c in text)print([c.isspace() for c in text])print([int(c.isspace()) for c in text])print(spaces)Enter Text: Hello World this is StackOverflow[False, False, False, False, False, True, False, False, False, False, False, True, False, False, False, False, True, False, False, True, False, False, False, False, False, False, False, False, False, False, False, False, False][0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]4