Course 4.1Lesson 4 of 5
To look inside a text, find gives the position of the first match and minus one when there is none, rfind gives the last match, index does the same but raises a ValueError, and count says how many times. Tests such as isdigit, isalpha, isalnum, isspace, isupper and islower ask what kind of characters a text has. partition cuts a text at the first separator into three parts, which makes a line of the form key equals value easy to read, with a check for a missing separator or an empty key. Always check a line before you trust it, and turn the text of a number into a number only when it has digits.
Try the code and the simulations beside the lesson.
After this lesson you can
partition, andkey=value, and refuse the bad ones.You know in, which says whether a piece is in a text. Sometimes you need to know where.
Predict first. What will each line show? Then run it.
| Method | What it gives |
|---|---|
text.find("cat") | the position of the first match, or -1 if there is none |
text.rfind("the") | the position of the last match |
text.index("sat") | like find, but a ValueError if it is missing |
text.count("at") | how many times the piece appears |
For "the cat sat on the mat": find("cat") is 4, find("dog") is -1, rfind("the") is 15 (the second the), and count("at") is 3 (cat, sat, mat). Positions start at 0, as always, and they count letters, spaces included.
As with the search of a list, -1 means "not found", and you must test for it before you use it as a position. A very common mistake is to use text[text.find("x")] when there is no x: the answer -1 quietly gives the last letter.
A family of methods answers yes or no about all the characters of a text.
| Method | True when |
|---|---|
isdigit() | every character is a digit (and there is at least one) |
isalpha() | every character is a letter |
isalnum() | every character is a letter or a digit |
isspace() | every character is white space |
isupper(), islower() | the letters are all capitals, or all small |
The empty text is False for all of them. And a text with a minus sign or a decimal point, such as "-5" or "3.5", is not isdigit(), which surprises beginners. For numbers with a sign, use try and int() instead.
split cuts at every separator. Sometimes you want to cut only at the first one, into three parts: before, the separator, and after. That is partition.
"name=Asha".partition("=") gives ("name", "=", "Asha"), a tuple that you can unpack: key, sep, value = .... If the separator is missing, you get ("name", "", ""): the separator part is empty, which tells you.
The position of the = is also what find("=") gives, because the positions start at 0.
Here is a real job: read lines of the form key=value. Some lines are good. Some are broken.
At the end, the values that are made of digits become numbers, with isdigit() and int(). The value 9x is left as text, because it is not all digits.
Notice the care. A program that reads real files must expect broken lines, and keep going. The two checks (sep and key) are cheap, and prevent most of the trouble.
find without testing for -1.isdigit() on a number with a sign or a point. It is False.Name and name become two different keys.split("=") when the value may contain an =. partition cuts only at the first one."".isdigit() is False).Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
find, rfind and count, and the checker confirms the answers.find gives a position or -1. index raises an error. count says how many.isdigit, isalpha, isalnum, isspace, isupper and islower test the kind of characters.partition cuts at the first separator into three parts. An empty separator part means that it was missing.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
"banana".find("n") give? And "banana".rfind("n")?"12.5".isdigit() give?"abc".partition("=") give?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.