Using len for text but discarding spaces in the count

后端 未结 7 1302
天命终不由人
天命终不由人 2020-12-10 20:44

So, I am trying to create a program which counts the number of characters in a string which the user inputs, but I want to discard any spaces that the user enters.



        
相关标签:
7条回答
  • 2020-12-10 21:15

    Some code as close as possible to your original:

    def main():
        full_name = input("Please enter in a full name: ").split()
        total = 0
        for x in full_name:
            total += len(x)
        print(total)
    

    However, I think len(full_name) - full_name.count(' ') is better.

    0 讨论(0)
提交回复
热议问题