String methods

Author

Clayton Cafiero

Published

2025-06-06

Some more string methods

We’ve already seen .upper(), .lower(), and .capitalize() but there are many more string methods. Here are a few that you’re likely to find quite useful.

.split()

It’s often the case that we want to divide a string into smaller parts like words or sentences. Of course, we can split a string into individual symbols using the list constructor:

>>> s = "My wombat has indigestion."
>>> list(s)
['M', 'y', ' ', 'w', 'o', 'm', 'b', 'a', 't', ' ', 'h', 'a', 
 's', ' ', 'i', 'n', 'd', 'i', 'g', 'e', 's', 't', 'i', 'o', 
 'n', '.']

But what if we wanted to split this into words rather than symbols? .split() to the rescue! .split() will, by default, split a string wherever it encounters whitespace. Example:

>>> s = "My wombat has indigestion."
>>> s.split()
['My', 'wombat', 'has', 'indigestion.']

Notice what’s going on here. The .split() method has returned a list of portions of the original string that had been separated by whitespace. The original string is unchanged. This is useful if you want to work with the words in a sentence or other string.

You may ask, what happened to the whitespace? Python discards whatever you use to split the string—in this case, whitespace.

If we have a multi-line string we can split it into words just the same.

>>> s = """Wombats are largely herbivorous.
... They eat grasses, shrubs, and other plants.
... They'll also eat roots and bark.
... They've been known to eat certain fungi.
... If they eat Gladys' cooking they'll get indigestion."""
>>> s.split()
['Wombats', 'are', 'largely', 'herbivorous.', 'They', 'eat', 
'grasses,', 'shrubs,', 'and', 'other', 'plants.', "They'll", 
'also', 'eat', 'roots', 'and', 'bark.', "They've", 'been', 
'known', 'to', 'eat', 'certain', 'fungi.', 'If', 'they', 
'eat', "Gladys'", 'cooking', "they'll", 'get', 
'indigestion.']

We can also split a multi-line string into individual lines, by providing the newline character as an argument to .split().

>>> s.split("\n")
['Wombats are largely herbivorous.',
 'They eat grasses, shrubs, and other plants.', 
 "They'll also eat roots and bark.", 
 "They've been known to eat certain fungi.", 
 "If they eat Gladys' cooking they'll get indigestion."]

By default, .split() splits on any whitespace: ' ', '\n', '\t', etc. If we want to be more specific or split on somthing entirely different, we can provide an argument to .split(). In the example above, s.split("\n") splits s on the newline symbol, and that gets us individual sentences.

You can split on just about anything:

>>> s = "tussock grass|kangaroo grass|bonfire moss"
>>> s.split("|")
['tussock grass', 'kangaroo grass', 'bonfire moss']

.join()

Sometimes we wish to assemble a string from individual parts—in essence the opposite of split. For this, we have .join(). Join acts on some string, called the glue, and uses that glue to connect parts provided as a list or tuple.

For example, imagine we had a list of tasty things for a wombat to eat (borrowed from the previous example):

>>> tasty_things = ['tussock grass', 'kangaroo grass', 
... 'bonfire moss']
>>> " and ".join(tasty_things)
'tussock grass and kangaroo grass and bonfire moss'

Here, the glue is the string " and " and .join() takes the elements of the list tasty_things and joins them together with the glue. The glue can be any string at all.

Sometimes we want to concatenate a list of strings without anything between them at all. For this, we use the empty string as the glue!

>>> letters = ['w', 'o', 'm', 'b', 'a', 't']
>>> "".join(letters)
'wombat'

.replace()

If you’ve ever used search and replace to replace some string in a word processing document, this one will seem familiar. Given some string, we can replace all occurrences of a given substring with another using .replace().

>>> s = "My wallaby has indigestion."
>>> s.replace('wallaby', 'wombat')
'My wombat has indigestion.'

Notice that s remains unchanged because strings are immutable. .replace() returns a copy of the string, with the requested replacements. So if you want to use this to update s (or whatever your string is named), you’ll need to perform an assignment.

>>> s = "My wallaby has indigestion."
>>> s = s.replace('wallaby', 'wombat')
>>> s
'My wombat has indigestion.'

Comprehension check

  1. Given the string 'New South Wales', how would you split this to produce the list ['New', 'South', 'Wales']?

  2. Given the string 'New South Wales, Queensland, South Australia, Tasmania, Victoria, Western Australia', how would you split this to produce the list ['New South Wales', 'Queensland', 'South Australia', 'Tasmania', 'Victoria', 'Western Australia']?

  3. Given the list ['New', 'South', 'Wales'], how would you join the elements of the list to produce the string 'New South Wales'?

  4. Given the string 'New South Whales', how would you construct the string 'New South Wales'?

  5. Given the string 'No Sloth Whales', how would you construct the string 'New South Wales'?

  6. What’s the result of the following:

    1. "A wallaby is like a wee tiny kangaroo.".split()?
    2. " ".join("A wallaby is like a wee tiny kangaroo.".split())?

Check your answers in the Python shell.

Copyright © 2023–2025 Clayton Cafiero

No generative AI was used in producing this material. This was written the old-fashioned way.