How to not count the "spaces" of a sentence in python

Asked

Viewed 726 times

1

I wonder how to count only the typed letters without space. Even with the strip it counts the spaces.

nomed = nome.split()
numdi = len(nomed[0])
print('O seu nome tem  letras {} e o seu primeiro nome tem {} letras'.format(nnome, numdi))``` 

1 answer

0


The strip method only removes the spaces at the beginning and end of the string.

In this case, the ideal is to use the replace method, which will replace all spaces with a chosen character (or no character in this case), as follows.

nnome = len(nome.replace(" ",""))
nomed = nome.split()
numdi = len(nomed[0])
print('O seu nome tem  letras {} e o seu primeiro nome tem {} letras'.format(nnome, numdi))
  • Thank you very much! It worked out by the method you suggested.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.