Using len for text but discarding spaces in the count

后端 未结 7 1308
天命终不由人
天命终不由人 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:01

    Why can't you just do:

    >>> mystr = input("Please enter in a full name: ")
    Please enter in a full name: iCodez wrote this
    >>> len(mystr.replace(" ", ""))
    15
    >>> len(mystr)
    17
    >>>
    

    This gets the length of the string minus the spaces.

提交回复
热议问题