Manipulating Strings With String Methods
00:00 Strings come bundled with special functions—they’re called string methods—that you can use to work with and manipulate strings. There are numerous string methods available, as you’ll see. First, you get to focus on a handful of commonly used ones.
00:17 In this lesson, you’ll learn how to convert a string to uppercase or lowercase, how to remove whitespace from a string, and how to determine if a string begins or ends with certain characters. Okay, time to learn your first method.
The first one is going to work on converting the case of the string. That method is
.lower(). It’ll convert the string to all lowercase letters, taking any capital letters and turning them into lowercase.
So it looks something like this. If you add the string of
"Jean-Luc Picard", and then you add
.lower() as a method, it’ll return all lowercase characters. The
. tells Python what follows is the name of a method, in this case the
If you take the same variable,
name, and apply the
.upper() method, you’ll see that it returns all capital letters. So just one more comparison between methods and functions that you saw previously: string methods will begin with a
. at the beginning of their names, and that’s how you’ll refer to them, with the leading “dot.” So, “dot lower” or “dot upper.” That’ll make it a little easier to differentiate functions that are string methods from built-in functions like
len(). When you compare them, aside from the different results of these functions, the important distinction is how they’re used.
These built-in functions are just called by themselves. So if you want to find the length of the
name string, you call
len() and put
name inside of it, or if you want to print
name, you would do the same thing vs how on the other hand, with
.lower(), they need to be used in conjunction with a string.
03:18 Sometimes you need to remove whitespace from the beginning or end of a string. This is especially useful when working with strings that come from user input, something that someone’s typed in, which may sometimes include extra whitespace characters by accident.
There are three string methods that you can use to remove this whitespace from a string:
.rstrip() removes trailing spaces from the right side of the string,
.lstrip() removes preceding spaces from the left side of the string, and
.strip() removes whitespaces from both left and right sides, so anything at the beginning or at the end of the string would be removed.
First, we need to create some whitespace, so I’ll make a new
name, put a couple spaces and a tab, and I’ll put my own name in again. You can use your own name. In this case, I’m going to add a couple spaces and a tab, and then
\n which is a line feed. Actually, I’ll add two of those line feeds with a couple spaces in between. So there’s a lot of different whitespace there. In fact, if you try out
print() with name, you’ll see all the white space, including the additional line feeds, the two of them. Okay, so what happens if you use
.rstrip()? It’s stripped away all the whitespace characters from the right side, including the line feeds. Again,
name’s still the same, so if I do
.lstrip(), that will get rid of the whitespace at the front.
Let’s say we have the string
"Enterprise". We’ll create a variable called
starship and capitalize
"Enterprise". If you use the method
.startswith(), and in this case, you need to, you can see it requesting what you’d like that prefix to be, and what if I put lowercase
"en"? Will that work? That returns
You might have figured out why—because it starts with a capital
"E", the fact that the
.startswith() method is actually case sensitive. To get this to be
True, you’d have to type the same thing, but start with a capital
"En". That would return true.
Do you remember talking about in a previous lesson, how strings are immutable? Most string methods alter a string like
.lower(), but they actually return copies of the original string with those modifications.
So if you aren’t careful, you might not recognize this, and it might introduce subtle bugs into your program. Try this one out. So put a name. This time, we’ll say
"Francis". Okay, so again, if you type
name and put in the method
.upper(), you get all uppercase, but you might have noticed, I showed this a little earlier, too, how the original is not modified. Now to get it to change the variable
name, you would need to reassign it.
07:31 You might have noticed that there are a lot of methods associated with strings. The methods that I’m showing in this lesson barely scratch the surface. IDLE can help you find these new string methods.
Let me show you how you can discover some of them. Let’s go back to our
starship example. If you start with the variable name,
starship, and then add the dot, a little box will pop up and start to show you some of the different ones that are available. If you type a letter, you’ll also get some of them. It’ll narrow down the results. In fact, if I start to spell
upper, you’ll see it highlighted there.
08:26 Okay, so now that you’ve practiced a little bit trying to find some of those additional methods, here’s a quick review using IDLE. You can type the name of the variable that’s assigned to the string, and then you add a period, often spoken as “dot,” and then you wait.
08:41 Typing in a letter will narrow in some of the results, and pressing Tab will automatically fill in that method name if you’ve started to name it. And one note, this even works with variable names.
09:12 Okay, time to review what you’ve learned. Here’s a couple exercises for you to try out. Write a program that converts the following strings to lowercase, then print each lowercase string on a separate line.
09:28 Try repeating exercise one, but convert each string to uppercase instead of lowercase. Write out a program that removes whitespace from the following strings, then print out the strings with all of the whitespace removed.
Using the same four strings as exercise 4, write a program that uses string methods to alter each string so that the
True for all of them. So again, write a program that uses other string methods to alter the strings so that
True for all of them.
Become a Member to join the conversation.