Kaugs
Kaugs

Reputation: 11

Dealing with a string that consists of only space characters

Genre = str(input(prompt))
GenreStripped = Genre.strip()
Length = len(GenreStripped)
while Length > 0:
        

Im trying to make a function that does not only accept ' ' spaces as an input, what are some other python functions to deal with ' ' as an input, because the strip() function does not strip ' '.

Upvotes: 1

Views: 39

Answers (1)

Jamie.Sgro
Jamie.Sgro

Reputation: 882

.strip() only removes leading and trailing whitespace, not spaces in between characters. you could use .replace(" ", "") instead if you wanted to remove every space

Try running the following to get a sense of the differences between these methods:

print(" test test test ".replace(" ", ""))  # 'testtesttest'
print(" test test test ".strip())  # 'test test test'

Upvotes: 1

Related Questions