Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
The string object in Python supports a lot of standard methods. We're going to take a look at just some of them here. We'll start with a string. I'm going to go ahead and put it in a variable and say s = 'this is a string'. And notice that's all lowercase, and we just look at the string by itself. It looks like that. If I say capitalize(), then we get it with just the first letter capitalized. I want all the letters capitalized I can say .upper() and there is certainly a lot of use for that.
Likewise, I can say .lower(). Of course it was all lowercase before, so I had 'THIS IS A STRING' in all uppercase I can say .lower(). And we'll see that we get the lowercase version of that. We can also say swapcase(). So if I had 'This Is A String,' sort of title case-ish, I say .swapcase(). Then I get it in the opposite case. If I take our string, which says, 'this is a string' and I want to find a particular word in it, I can look for it say the word "is." And we will find it at position 2.
And you might look here and you might say, well, "is" is at position 0, 1, 2, 3, 4, 5, but in fact, it found this. So it's looking for exactly that string. In the first occurrence of it is the one that get will return. And so the first letter is 0, the second letter is 1, and then "is" starts at position 2. We can also do string replace. I can say s.replace, this for that. And it'll say 'that is a string'.
Now, it's important to note that the string itself is immutable. So the string has not changed with these functions do as they return a different string. And it's an entirely different string object that gets returned. For example, if I look at the id of (s) I'll see that its id is that number there. And if I say newstring = s.upper(), I now have a new string which is the uppercase string. It's that different string that was returned by the upper() method.
If I look at the id of (newstring), you'll see that it's entirely different than the id of 'this is a string'. So there's few more useful ones, there is a strip. I say s.strip(). What strip does is that strips a particular sequence of characters from both the end of the string and the beginning of the string. By default what it strips is whitespace. So if I have a string that says ' this is a string ' like that, with a bunch of whitespace at the beginning and the end of it, and I say .strip(), I'll get back the string without that whitespace at the beginning and the ending of that.
And so where this very commonly used for is removing new lines, but you need to be aware when you use it for that, that it will also remove any leading space. If you want to just remove whitespace from the end of string, you can use rstrip(). And that will remove the whitespace from the end of a string and not change anything at the beginning of the string. And if you just want to remove a new line, let's say we have a string, I'm going to put this in a variable because we need it a couple of times, and say s1 = 'This is a string\n' with the newline at the end of it.
I said sting instead of string. That will work. And there we see we have the string with a newline at the end of it. If I say s1.strip(), of course, it'll remove the new line. And that's very useful, but it will also remove any whitespace from the beginning or the end. If I just want to remove the new line from the end, I can actually say s1.rstrip and specify the newline. So whatever string you specify, parenthesis is what will be stripped. And there we have it with just a newline taken from just the end of the string.
There's also a set of methods for testing the content of a string really quickly. We'll look at these. We can say s.isalnum(). And what that is, is that it checks if the string has only alphanumeric characters in it. And of course, this is False because it also had spaces in it. If I have a string that looks like 'thisisastring' and has no spaces in it and I say .alnum() and it'll come up True. I must have spelled something wrong. I did. 'thisisastring'.isalnum() and that comes up True.
So these methods that start with "is," there is a bunch of them. I'm just going to show you a few. There's isalpha(), 'thisisastring'.isalpha(). And that checks just for alpha characters. So that would be the letters a-z or other alpha characters in other text encodings beside just ASCII. And there is also one for checking if it is digit. And so if I have a string that's just digits and say isdigit(), that one will come up True, but if I check s.isdigit() it will of course come up False.
And finally there's isprintable(), which checks to see if all the characters in the string are printable. So those are some of most common and most popular methods that are available for the string class.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 96436 Viewers
56 Video lessons · 110180 Viewers
71 Video lessons · 78962 Viewers
131 Video lessons · 37884 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.