Course 2.2Lesson 2 of 5
A slice such as word[2:5] takes the characters from position 2 up to, but not including, position 5. word[:3] starts at the beginning, word[3:] goes to the end, and word[::-1] gives the word backwards. String methods such as upper(), lower(), title(), strip() and replace() give a new, changed string and leave the old one as it was. strip() is useful to clean the spaces that a user types by mistake.
Try the code and the simulations beside the lesson.
After this lesson you can
A slice takes a piece of a string. It is written with two numbers and a colon inside square brackets: word[start:stop]. The piece starts at start, and goes up to, but not including, stop.
The picture shows the first slice. Think of the numbers as being between the characters, at their left edges. word[2:5] cuts at 2 and at 5, and takes what is between. This is why word[:3] + word[3:] is always the whole word again: the two cuts meet at 3.
A slice never gives an IndexError. If the numbers are too big, Python simply takes what exists: "cat"[1:100] is at.
This is the same rule as range(): start included, stop not included.
A method is a function that belongs to a string. We write it after a dot: text.upper(). Here are the ones that you will use most.
| Method | What it does | Example |
|---|---|---|
upper() | all capital letters | "hi".upper() is "HI" |
lower() | all small letters | "HI".lower() is "hi" |
title() | a capital letter at the start of each word | "python is fun".title() is "Python Is Fun" |
capitalize() | only the first letter of the text is a capital | "python is fun".capitalize() is "Python is fun" |
strip() | takes away spaces at the two ends | " hi ".strip() is "hi" |
replace(a, b) | changes every a into b | "cat".replace("c", "b") is "bat" |
Because each method gives a string, you can write another method after it. text.strip().title() first strips and then makes a title. Read it from left to right.
A very useful line when you read a name from the user: name = input("Name: ").strip().title(). It takes away the spaces that were typed by mistake, and it fixes the capital letters. So " aSHA rao " becomes "Asha Rao".
name.upper() on a line by itself does nothing that you can see. Write name = name.upper().name.upper without () is not an error, but it does not give the text. It shows something like <built-in method upper of str object>.word[2:5] the character at position 5 is not in the slice.word[5:2] is empty, because the start is after the stop.title() on text with apostrophes. "it's".title() is "It'S". Be careful with such words.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:
word[start:stop] takes the characters from start up to, but not including, stop.word[:3] and word[3:]. A step of -1 reverses.upper(), lower(), title(), strip() and replace() give a new string.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
"python"[1:4], "python"[:2] and "python"[-2:]?"python"?" hi ".strip().upper() give?text.upper() change text?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.