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.
JavaScript
x
9
1
def main():
2
full_name = str(input("Please enter in a full name: ")).split(" ")
3
4
for x in full_name:
5
print(len(x))
6
7
8
main()
9
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.
Advertisement
Answer
Count the length and subtract the number of spaces:
JavaScript
1
6
1
>>> full_name = input("Please enter in a full name: ")
2
Please enter in a full name: john smith
3
>>> len(full_name) - full_name.count(' ')
4
9
5
>>> len(full_name)
6