Skip to content
Lesson 12 of 13

Step 1 of 8 · Reading · ~4 min

Learn

Working with Text

String Methods

A string carries its own set of tools, called methods, for changing case, trimming, testing characters, and breaking text into words and gluing it back together. By the end you can take a messy line of input and turn it into exactly the clean text a program expects.

Calling a method

A method is a function attached to a value. You call it with a dot after the value and parentheses after the name:

python

The second line shows the rule that matters most: strings are immutable (you met this in the last lesson), so a method never changes drink. It hands back a NEW string. If you want to keep the result, assign it: drink = drink.upper().

Case and trimming

python

strip() removes spaces (and tabs and newlines) from both ends only. Spaces in the middle stay.

Testing one character at a time

These methods answer True or False about every character in the string:

callresultwhy
"q7".isalnum()Trueletters and digits only
"2B!".isalnum()False! is neither
" ".isalnum()Falsea space is neither
"x".isalpha()Truea letter
"5".isdigit()Truea digit

Combined with a for loop they let you keep only the characters you want. Here is a phone number reduced to its digits:

python

split: one string into many

split() cuts a string into pieces and returns a list, an ordered group of values written in square brackets. With no argument it splits on any run of whitespace and ignores whitespace at the ends:

python

That is the trap. split(" ") cuts at every single space, so two spaces in a row, or a space at either end, leave empty strings '' in the list. split() with nothing inside the parentheses is almost always what you want for words.

join: many strings into one

join goes the other way. It is called on the separator, and the list goes inside the parentheses:

python

The separator goes only BETWEEN items, never before the first or after the last.

Chaining

Each method returns a string, so you can call the next method on the result straight away. Read a chain left to right:

python

upper() runs first, then split() on what it returned.

Common mistakes

  • Splitting on a single space. "a b".split(" ") contains an empty string, so joining it gives two separators in a row: a--b instead of a-b. Leading or trailing spaces also put a separator at the very start or end.
  • Using replace(" ", "-") instead of splitting. " a b ".replace(" ", "-") is -a--b-: every space becomes a separator, including the extra ones.
  • Throwing away a method's result. text.lower() on a line by itself does nothing to text; you still print the capitals.
  • Turning a removed character into a separator. Dropping & from "x & y" leaves two spaces between the words. That must still come out as one separator, not two.

Your exercise

Turn a title into a URL slug: lowercase words joined by single hyphens, with every character that is not a letter, digit or space thrown away. The starter reads the title and prints it unchanged. The mistake to watch for is doubled or dangling hyphens where the title had extra spaces or punctuation.

Up nextString FormattingWorking with Text

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…