Saturday 16 May 2015

Removing Unwanted Whitespace in a String in Python 3

I have written a simple chatbot program in Python 3 where the user enters a comment and the bot responds. After offering it to family to play with I discovered that they would often put whitespace in front of the sentence or after. For example:

'  How are you?     '  or  'How are you? '
Problem:
The problem is that when I send this to the file to search for the match response. It comes up without a match because the string is saved as:
 'How are you?'
As you can see this has no spaces before or after. The single quotation marks. So when I run the search with it comes up with nothing and I don't really want to add unessessary lists with spaces just in case the program receives sloppy input.

Solution:
It turns our that the solution is quite simple. Python provides two simple tools to remove extra white space before and after the sentence or even duplicate spaces between words in a string.

In the first function, remove_whitespace_either_side, we use the variable.strip() method. This method essentially strips the white space from either side of the sentence in the string.

But what happens if your program user is a bit of a typing numpty and manages to put extra white spaces between the works? For example:
' I am     a typing                   numpty!'
As you can see, the first function cannot deal with this.

This is where the second function, remove_any_duplicated_white_space , is so useful. This function uses two methods forming " ".join(variable.spit()). Here, we split all the words into individual strings and then join them back together into one string with a single space between each.

You can see the result of each in the code above.

Looking at the quality of input I have been getting for my chatbot I think I will be using the second function.

No comments:

Post a Comment