Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using len for text but discarding spaces in the count

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.

def main():
    full_name = str(input("Please enter in a full name: ")).split(" ")

    for x in full_name:
        print(len(x))


main()

Using this, I can get the number of the characters in each word, without spaces, but I don't know how to add each number together and print the total.

like image 321
Izento Avatar asked Oct 29 '13 20:10

Izento


1 Answers

Count the length and subtract the number of spaces:

>>> full_name = input("Please enter in a full name: ")
Please enter in a full name: john smith
>>> len(full_name) - full_name.count(' ')
9
>>> len(full_name)
like image 163
inspectorG4dget Avatar answered Nov 08 '22 21:11

inspectorG4dget