Introduction to Python Resources

Begin your free Python journey now,
at your own pace

check

Syntax: String Cleaning

check

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 len method is a property of the stringVal variable
    • 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.
  • the return value of the len() string method is being stored in the stringLength variable.

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:

  1. 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.

  2. Using the correct string method above, remove the whitespace from the left side of the string_to_clean variable and assign it to a new variable named cleaned_left.

  3. Print the string value contained in the cleaned_left variable.

  4. Using the correct string method above, remove the whitespace from the right side of the string_to_clean variable and assign it to a new variable named cleaned_right.

  5. Print the string value contained in the cleaned_right variable.

  6. Using the correct string method above, remove the whitespace from both sides of the string_to_clean variable and assign it to a new variable named cleaned_string.

  7. Print the string value contained in the cleaned_string variable.

    HINT*: Click here for a hint
    *Hints are best viewed after completing a task, or after spending some time and effort attempting it. In cases where a hint contains example output, it is important to understand why it is correct, as there may be many correct outputs.
    > "surrounded by space   "
    > "   surrounded by space"
    > "surrounded by space"
    SOLUTION*: Click here for an example solution
    *Example solutions are best viewed after completing a task and understanding the outcome. In most cases, there are multiple ways to complete a task, and the example solution is only one example.
    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:

  1. Create a new variable named random_case and assign it a string value "UpSaNdDoWnS".

  2. Using the correct string method above on the random_case variable, convert all letters of the random_case variable to lowercase and save the updated string to a new variable named lowercase.

  3. Print the string value contained in the lowercase variable.

  4. Using the correct string method above on the random_case variable, convert all letters of the random_case variable to uppercase and save the updated string to a new variable named uppercase.

  5. Print the string value contained in the uppercase variable.

  6. Create a new variable named text_phrase and assign it a string value "programming: a way of life"

  7. Using the correct string method above on the text_phrase variable, convert the p in programming to uppercase and save the updated string in a new variable named capital_p.

  8. Print the string value contained in the capital_p variable.

  9. Using the correct string method above on the text_phrase variable, convert the first letter of each word to uppercase and save the updated string in a new variable named title_case.

  10. Print the string value contained in the title_case variable.

    HINT*: Click here for a hint
    *Hints are best viewed after completing a task, or after spending some time and effort attempting it. In cases where a hint contains example output, it is important to understand why it is correct, as there may be many correct outputs.
    > "upsanddowns"
    > "UPSANDDOWNS"
    > "Programming: a way of life"
    > "Programming: A Way Of Life"
    SOLUTION*: Click here for an example solution
    *Example solutions are best viewed after completing a task and understanding the outcome. In most cases, there are multiple ways to complete a task, and the example solution is only one example.
    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!