14. Working with text

Chapter 12 covered the everyday string tools: joining with + and f-strings, length with len(), upper and lower case, repeating, and formatting. This chapter adds the tools for looking inside a string: slicing it, finding something in it, and replacing part of it. In Python, all of these are methods built directly onto string objects — no separate library needed.

Slicing with s[i:j]

Python uses slice notation to extract part of a string. The syntax is s[start:stop], where start is the index of the first character you want and stop is the index after the last one:

print("hello"[0:3])   # hel
print("hello"[2:])    # llo   (from index 2 to the end)
print("hello"[-2:])   # lo    (last 2 characters)

Three things to notice:

  • Indexes start at 0, not 1. The first character is at index 0, the second at 1, and so on.
  • Leaving out stop means "to the end of the string".
  • A negative index counts from the end: -1 is the last character, -2 the second-to-last, and so on.

Like every string operation, slicing returns a new string and leaves the original alone.

Python strings are 0-indexed. If you want the first character, use s[0], not s[1]. Asking for s[1] gives the second character. This will feel wrong at first; it becomes natural quickly.

Open exercises/14/01-slice.py. A variable holds a word. Using slicing, print just its first letter, then just its last.

Searching with in and .find()

The simplest way to check whether a string contains something is the in operator, which returns True or False:

print("world" in "hello world")   # True
print("z" in "hello world")       # False

Because it returns a boolean, in pairs naturally with if:

sentence = "the password is swordfish"
if "sword" in sentence:
    print("Found it.")
else:
    print("Not in there.")

When you also need to know where the match is, use .find(sub). It returns the index of the first occurrence, or -1 if not found:

print("hello world".find("world"))   # 6
print("hello world".find("z"))        # -1

Python's .find() returns -1 on a missing match, not None. To test for presence, use in — it is clearer. Use .find() when you need the position number.

Replacing with .replace()

.replace(old, new) returns a new string with every occurrence of old swapped for new:

print("hello".replace("l", "L"))         # heLLo
print("a-b-c".replace("-", " "))         # a b c

The original string is untouched. If you want to keep the result, store it:

message = "meet me at noon"
dashed = message.replace(" ", "-")
print(dashed)   # meet-me-at-noon

Other useful string methods

Python strings have many methods. A few you will reach for often:

Method What it does
s.strip() Remove leading and trailing whitespace.
s.split() Split on whitespace, return a list of words.
s.split(sep) Split on a specific separator.
s.startswith(t) True if s starts with t.
s.endswith(t) True if s ends with t.
padded = "  hello  "
print(padded.strip())              # hello

words = "one two three".split()
print(words)                       # ['one', 'two', 'three']

parts = "a,b,c".split(",")
print(parts)                       # ['a', 'b', 'c']

Homework

Homework files are in exercises/14/homework/.

Problem 1 — Initials

Open exercises/14/homework/01-initials.py. Two variables hold a first and last name. Using slicing (s[0]), print the person's initials, like K.R. for "Keiko Raharja".

Problem 2 — Contains

Open exercises/14/homework/02-contains.py. A variable holds a sentence. Use the in operator inside an if to print yes if it contains the word python, and no if it does not.

Problem 3 — Censor

Open exercises/14/homework/03-censor.py. A variable holds a short message. Use .replace() to swap every space with a dash -, then print it.

Challenge — Last word length

Open exercises/14/homework/04-last-letter.py. A variable holds a word. Using a negative index in a slice, print its last three characters, then print its length with len(). Make it work for any word without counting by hand.

Stuck or finished? Open the homework solutions page.