After remaining in drafting for over a year (initial draft was on 7 August, 2013), I have finished this new Python tutorial. There is a lot to be written about case normalization, both for the upsides and downsides, but it is generally agreed upon that user input should be normalized, or converted into such a form that any checks run on the input can be case-insensitive. This tutorial shows how you can do that and provides a through example of case normalization in action. No, just because I use the term “in action” does not mean it is your favorite or the newest theatrical action movie. 😛
It is important to normalize user input in your program, but what exactly is normalization and how do you do it? That is the topic of the Python tutorial you are about to read.
While all code should be Python 2 and 3 compatible unless otherwise noted, any
Exceptions are from Python 3.3+.
When you ask for user input and are checking it against or for something, such as if it matches a value or it contains a certain letter, it is a good idea to normalize the input, or in other words make it case-insensitive. Normalization improves the user experience by not forcing them to type certain text exactly as displayed as well as makes programming easier for you by not having to individually check for each possible case.
For clarification, normalization is different from sanitation, where potentially dangerous, unusable, or non-displayable characters are removed or converted into valid characters. 😉
To normalize input, all you have to do is append
str.upper() where ever you check the input or to the
input() (Python 2
raw_input()) function directly. These two functions (technically methods of the
str object) convert a string to lowercase or uppercase, respectively.
Personally, I use and recommend
str.upper(), primarily because I like to use lowercase checks for input and because the latter may not always work (see the note in the Python docs). However, sometimes your code design will dictate which you use as well as personal preference.
In order to better understand the power of case normalization properly and how to do it yourself, I have written the following Python script demonstrating the concept and explaining what is going on. I suggest reading it all the way through first to understand the logic, then running it multiple times using different cased input and observing the results.
The advantages of normalized user input are clear, and should be done at every possible chance. There may be times you need to have one-certain-answer-and-no-more input, and in that circumstance you would not normalize the input. In most cases, however, you will normalize the input, the end. 😛
Now my readers, go improve your user’s experience using the power of normalization, and never look back at the exact case checks you once used. Your skill as a programmer has increased, and truthfully, you will never return to that dark time, simply because the new way is full case normalized light. 😀