String methods

Learn this with friends

ComputingIntro to computer science - Python5:286 check-ins

About this video

Python provides built-in string methods, special functions attached to the string type and called with dot syntax, like "bananas".count("a"). The video covers lower and upper, strip, split and join, including uses like case-insensitive matching and processing CSV rows. Because strings are immutable, string methods never modify the original string; they return a new one.

You'll be able to

  • call a string method using dot syntax.
  • use lower, upper, strip, split and join to process text.
  • explain why I must store the return value of a string method.
  • split a CSV row into fields and join a list of strings back together.

Key words

method
A special kind of function attached to a type and called with dot syntax.
dot syntax
Calling a method by writing the value, a dot, then the method name, like s.lower().
immutable
Cannot be changed after it is defined; strings are immutable.
white space
Invisible characters like spaces, tabs and line breaks.
CSV
Comma-separated values: text where each line is a row and values are separated by commas.
separator
The string used to split text apart or placed between items when joining.
Transcript

- [Instructor] Now that we know we can iterate over the characters of a string, we have the power to perform all kinds of operations. We can count how many times a specific character appears in a string, or replace all occurrences of one character with another.

Of course, we can always write our own function that iterates over the string and performs that count operation. But why not let someone else do the work? For common operations like this, Python provides built-in functionality in the form of string methods. A method is just a special kind of function.

All methods are functions, but not all functions are methods. Instead of packaging our functionality into arbitrary modules, we can attach them directly to the type that they operate on. In this case, the string type. A string knows how to count, replace, lower, strip, but an in integer type does not.

The major visible difference with methods is that we call them using dot syntax. Instead of passing the string we wanna operate on as an argument to the method, we call the method on that string. So we have the string.method name, parentheses, and then any additional arguments we wanna pass in.

So this counts how many times the character, a, appears in the string, "bananas". The string before the dot is also being passed into the method, but in its own special way. We'll trace how this works in more detail when we learn to write our own types and our own methods.

For now, the important thing to remember is that the string to operate on goes before the dot. Whew! Okay, all that syntax out of the way, what string methods does Python give us? To start, we have the lower and upper methods. These take the string and convert it to either all lowercase or all uppercase.

Any number or symbol characters, it leaves the same. Now, you might recall that strings are an immutable type, which means the computer can't mutate or change the characters of a string after it's defined. That means the lower and upper methods, like all string methods, do not modify the original string.

They return a new string. So I need to store the return value either in a new variable or back in the same variable. The lower method can be especially useful for case insensitive matching. That is, if we wanna consider all different capitalizations of a string as equivalent.

We convert all the strings to lowercase first and then compare them. We also might wanna normalize our string by removing any irrelevant punctuation at the start or end. For this, we have the strip method. The strip method strips away or removes any leading or trailing occurrences of the given characters.

It doesn't remove all occurrences, only the ones at the start or end of the string. So this would remove any combination of periods, question marks, and exclamation marks that appear on either end. If we don't pass in any arguments to the strip method, by default, it removes any leading or trailing white space.

Note that white space includes more than just space characters. Some strings can have tabs or line breaks. So this and this are not equivalent. Let's talk about two more common string methods, split and join. The split method splits up a string into a list of strings.

By default, it splits based on white space. So we can take a text and split it into a list of words. We can also pass in an argument to specify a different separator, like we could split based on the substring, "the". Another common use case for the split method is CSV strings, which stands for comma-separated values.

When you download tabular data from a spreadsheet tool, the computer typically puts it in CSV format. A CSV file is essentially one giant string where each line represents a row in the table. Within each row, the individual values or columns are separated by commas.

We can speed up a lot of analyses by downloading the CSV file and then writing our own program to process it. To get a list of the individual data fields in a row, we can call the split method with a comma separator. But what about putting that string back together?

We call the join method on a separator string and pass as an argument a list of strings. The join method joins or concatenates all the strings in the list into a single string, inserting that separator string in between each one. So to join our CSV row back together, we might call the join method on a comma string.

We also commonly call this on a space character to join a list of words back together. But we can call the joy method on any arbitrary separator string we want. Just note that this still is a string method, so we need to call it on the string, not the list. In total, Python has over 40 different string methods, but lower, upper, strip, split, and join are likely to be some of your most commonly used.

If you find yourself writing a function that operates on a string in some other standard way, you should look up if there's a string method for it. Just remember to call it using dot syntax. And keep in mind that string methods never modify the original string.

Video © Khan Academy, used under CC BY-NC-SA. Watch on Khan Academy. Puzzicle is not affiliated with Khan Academy.