Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
310 views
in Technique[技术] by (71.8m points)

python - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...