pythonvariables

Is there any way to exclude the spaces when writing the full name in python?


full_name = input("What is your name?")
length_of_full_name = len(full_name)
print(length_of_full_name)

Here, if the user inputs a name, then usually the len() function considers all the string of text, including the spacebar. For example, if I enter a name: John Smith It shows 10 characters, instead of the nine characters in the name. How do i make python ignore the space in between the name and the surname? Do I have to use any built-in functions, or do I have to use arrays? I am a python beginner and so I need an answer fast. I will truly be grateful if someone gives me the answer to the question.


Solution

  • Use a split function before and a combine function after, like here:

    # existing code...
    name_list = full_name.split(' ')
    name_remspace = "".join(name_list)
    
    # Use: len(name_remspace)
    

    Hope this helped! I'm a python beginner too, so my method might not be the best.