
Syntax: String Cleaning
Cleaning strings with Python built-in string functions.
Cleaning Python Strings
String manipulation is a very common part of working in Python, and often strings are not in the exact format we need them to be in. That's where string cleaning functions become handy. In this exercise you will be shown some very useful tools for cleaning up strings.
This exercise also brings up another really important topic, and that is methods. A method is a function that is attached to an object. What this means is that when you want to use one of the string methods listed below, you will need to call those methods on a string.
The question that usually comes up here is how do we call methods vs calling functions in python? To help with the explanation on calling methods, we will use a function we are already very familiar with, the print() function.
As we already know, the print() function is always available and can be called in this way:
print(value_to_print)
Notice above that the syntax for calling the print() function is the name of the function, a pair of parentheses (), and in this case, a value to print called an argument.
Now in contrast, let's see how a method is called. In this example we will use a method named len():
stringVal = "tester"
stringLength = stringVal.len()
Notice the following about the above:
- stringVal is a variable which is assigned a string value of
"tester" - the
lenmethod is apropertyof thestringValvariable- You can tell because of the
.on the end of the stringVal variable. This (.) is called (dot notation) and it is used to access values stored on objects.
- You can tell because of the
- the
return valueof thelen()string method is being stored in thestringLengthvariable.
The big thing to remember here is that a method needs to be called on an object, and a string method like len() and the other methods in this exercise need to be called on a string value.
Now, let's learn about some useful string methods!
Removing Excess Whitespace
"Whitespace" characters are characters that take up space, but aren't directly visible. On a white background, they are completely white, hence the name "whitespace". The most common examples are:
- A space character (
" "). Added to text when pressing the Spacebar. - A tab character (
"\t"). Added to text when pressing the Tab key. - A newline character (
"\n"). Added to text when pressing the Enter/Return key.
This extra whitespace often shows up at the beginning or end of a string. Python has dedicated methods for removing it:
lstrip(): Removes whitespace from the beginning of any string value it is given as an argument and returns a new string without that whitespace.rstrip(): Removes whitespace from the end of any string value it is given as an argument and returns a new string without that whitespace.strip(): Removes whitespace from both the beginning and end of any string value it is given and returns a new string without that whitespace.
print(" Elbow room ".lstrip())
print(" Elbow room ".rstrip())
print(" Elbow room ".strip())
> "Elbow room "
> " Elbow room"
> "Elbow room"
Note: Notice that all of the method calls above are calling the methods directly on a declared string. It is totally valid to use string methods on either string literals or variables that contain string values.
Practice: Stripping Whitespace
Based on the information above. Complete the following steps:
-
Create a variable named
string_to_clean, and assign it the value" surrounded by space ", making sure to include three characters of whitespace on both sides of the text. -
Using the correct string method above, remove the whitespace from the left side of the
string_to_cleanvariable and assign it to a new variable namedcleaned_left. -
Print the string value contained in the
cleaned_leftvariable. -
Using the correct string method above, remove the whitespace from the right side of the
string_to_cleanvariable and assign it to a new variable namedcleaned_right. -
Print the string value contained in the
cleaned_rightvariable. -
Using the correct string method above, remove the whitespace from both sides of the
string_to_cleanvariable and assign it to a new variable namedcleaned_string. -
Print the string value contained in the
cleaned_stringvariable.HINT*: Click here for a hint
> "surrounded by space "
> " surrounded by space"
> "surrounded by space"
SOLUTION*: Click here for an example solution
string_to_clean = " surrounded by space " cleaned_left = string_to_clean.lstrip() print(cleaned_left) cleaned_right = string_to_clean.rstrip() print(cleaned_right) cleaned_string = string_to_clean.strip()
Modifying a String's Casing
Sometimes, part of a string needs to be converted from uppercase to lowercase or vice-versa. There are a few built-in Python string methods for common conversions:
upper: converts all letters of a string value to uppercase and then returns the uppercase string.lower: converts all letters of a string value to lowercase and then returns the lowercase string.capitalize: converts the first character of a string value to uppercase (if it's a letter), and converts all remaining letters to lowercase and returns that new string.title: converts the first letter of each word in a string value to uppercase and the remaining letters to lowercase and returns that new string.
message = "I went to the store last week. It was grand!"
print(message.upper())
> "I WENT TO THE STORE LAST WEEK. IT WAS GRAND!"
message = "I went to the store last week. It was grand!"
print(message.lower())
> "i went to the store last week. it was grand!"
message = "I went to the store last week. It was grand!"
print(message.capitalize())
> "I went to the store last week. it was grand!"
message = "I went to the store last week. It was grand!"
print(message.title())
> "I Went To The Store Last Week. It Was Grand!"
Practice: Re-Casing Strings
Based on the information above. Complete the following steps:
-
Create a new variable named
random_caseand assign it a string value"UpSaNdDoWnS". -
Using the correct string method above on the
random_casevariable, convert all letters of therandom_casevariable to lowercase and save the updated string to a new variable namedlowercase. -
Print the string value contained in the
lowercasevariable. -
Using the correct string method above on the
random_casevariable, convert all letters of therandom_casevariable to uppercase and save the updated string to a new variable nameduppercase. -
Print the string value contained in the
uppercasevariable. -
Create a new variable named
text_phraseand assign it a string value"programming: a way of life" -
Using the correct string method above on the
text_phrasevariable, convert thepinprogrammingto uppercase and save the updated string in a new variable namedcapital_p. -
Print the string value contained in the
capital_pvariable. -
Using the correct string method above on the
text_phrasevariable, convert the first letter of each word to uppercase and save the updated string in a new variable namedtitle_case. -
Print the string value contained in the
title_casevariable.HINT*: Click here for a hint
> "upsanddowns"
> "UPSANDDOWNS"
> "Programming: a way of life"
> "Programming: A Way Of Life"
SOLUTION*: Click here for an example solution
random_case = "UpSaNdDoWnS" lowercase = random_case.lower() print(lowercase) uppercase = random_case.upper() print(uppercase) text_phrase = "programming: a way of life" capital_p = text_phrase.capitalize() print(capital_p) title_case = text_phrase.title() print(title_case)
When you have properly completed the above, you are ready to move on to the next exercise!