Next / Previous / Contents / TCC Help System / NM Tech homepage

3.3. String methods

Many of the operations on strings are expressed as methods. A method is sort of like a function that lives only inside values of a certain type. To call a method, use this syntax:

expr.method(arg1, arg2, ...)

where each argi is an argument to the method, just like an argument to a function.

For example, any string value has a method called center that produces a new string with the old value centered, using extra spaces to pad the value out to a given length. This method takes as an argument the desired new length. Here's an example:

>>> star = "*"
>>> star.center(5)
'  *  '

The following sections describe some of the more common and useful string methods.

3.3.1. .center(): Center some text

Given some string value s, to produce a new string containing s centered in a string of length n, use this method call:

s.center(n)

This method takes one argument n, the size of the result. Examples:

>>> k = "Ni"
>>> k.center(5)
'  Ni '
>>> "<*>".center(12)
'    <*>     '

Note that in the first example we are asking Python to center the string "Ni" in a field of length 5. Clearly we can't center a 2-character string in 5 characters, so Python arbitrarily adds the leftover space character before the old value.

3.3.2. .ljust() and .rjust(): Pad to length on the left or right

Another useful string method left-justifies a value in a field of a given length. The general form:

s.ljust(n)

For any string expression s, this method returns a new string containing the characters from s with enough spaces added after it to make a new string of length n.

>>> "Ni".ljust(4)
'Ni  '
>>> "Too long to fit".ljust(4)
'Too long to fit'

Note that the .ljust() method will never return a shorter string. If the length isn't enough, it just returns the original value.

There is a similar method that right-justifies a string value:

s.rjust(n)

This method returns a string with enough spaces added before the value to make a string of length n. As with the .ljust() method, it will never return a string shorter than the original.

>>> "Ni".rjust(4)
'  Ni'
>>> m = "floccinaucinihilipilification"
>>> m.rjust(4)
'floccinaucinihilipilification'

3.3.3. .strip(), .lstrip(), and .rstrip(): Remove leading and/or trailing whitespace

Sometimes you want to remove whitespace (spaces, tabs, and newlines) from a string. For a string s, use these methods to remove leading and trailing whitespace:

  • s.strip() returns s with any leading or trailing whitespace characters removed.

  • s.lstrip() removes only leading whitespace.

  • s.rstrip() removes only trailing whitespace.

>>> spaceCase = ' \n \t Moon   \t\t '
>>> spaceCase
' \n \t Moon   \t\t '
>>> spaceCase.strip()
'Moon'
>>> spaceCase.lstrip()
'Moon   \t\t '
>>> spaceCase.rstrip()
' \n \t Moon'

3.3.4. .count(): How many occurrences?

The method s.count(t) searches string s to see how many times string t occurs in it.

>>> chiq = "banana"
>>> chiq
'banana'
>>> chiq.count("a")
3
>>> chiq.count("b")
1
>>> chiq.count("x")
0
>>> chiq.count("an")
2
>>> chiq.count("ana")
1

Note that this method does not count overlapping occurrences. Although the string "ana" occurs twice in string "banana", the occurrences overlap, so "banana".count("ana") returns only 1.

3.3.5. .find() and .rfind(): Locate a string within a longer string

Use this method to search for a string t within a string s:

s.find(t)

If t matches any part of s, the method returns the position where the first match begins; otherwise, it returns -1.

>>> chiq
'banana'
>>> chiq.find ( "b" )
0
>>> chiq.find ( "a" )
1
>>> chiq.find ( "x" )
-1
>>> chiq.find ( "nan" )
2

If you need to find the last occurrence of a substring, use the similar method s.rfind(t), which returns the position where the last match starts, or -1 if there is no match.

>>> chiq.rfind("a")
5
>>> chiq[5]
'a'
>>> chiq.rfind("n")
4
>>> chiq.rfind("b")
0
>>> chiq.rfind("Waldo")
-1

3.3.6. .startswith() and .endswith()

You can check to see if a string s starts with a string t using a method call like this:

s.startswith(t)

This method returns True if s starts with a string that matches t; otherwise it returns False.

>>> chiq
'banana'
>>> chiq.startswith("b")
True
>>> chiq.startswith("ban")
True
>>> chiq.startswith ( 'Waldo' )
False

There is a similar method s.endswith(t) that tests whether string s ends with t:

>>> chiq.endswith("Waldo")
False
>>> chiq.endswith("a")
True
>>> chiq.endswith("nana")
True

The special values True and False are discussed later in Section 6.1, “Conditions and the bool type”.

3.3.7. .lower() and .upper(): Change the case of letters

The methods s.lower() and s.upper() are used to convert uppercase characters to lowercase, and vice versa, respectively.

>>> poet = 'E. E. Cummings'
>>> poet.upper()
'E. E. CUMMINGS'
>>> poet.lower()
'e. e. cummings'

3.3.8. Predicates for testing for kinds of characters

Use the string methods in this section to test whether a string contains certain kinds of characters. Each of these methods is a predicate, that is, it asks a question and returns a value of True or False.

  • s.isalpha() tests whether all the characters of s are letters.

  • s.isupper() tests whether all the letters of s are uppercase. (It ignores any non-letter characters.)

  • s.islower() tests whether all the letters of s are lowercase letters. (This method also ignores non-letter characters.)

  • s.isdigit() tests whether all the characters of s are digits.

>>> mixed = 'abcDEFghi'
>>> mixed.isalpha()
True
>>> mixed.isupper()
False
>>> mixed.islower()
False
>>> "ABCDGOLDFISH".isupper()
True
>>> "lmno goldfish".islower()
True
>>> "abc $%&*(".islower()
True
>>> "abC $%&*(".islower()
False
>>> paradise = "87801"
>>> paradise.isalpha()
False
>>> paradise.isdigit()
True
>>> "abc123".isdigit()
False

3.3.9. .split(): Break fields out of a string

The .split() method is used to break a string up into pieces wherever a certain string called the delimiter is found; it returns a list of strings containing the text between the delimiters. For example, suppose you have a string that contains a series of numbers separated by whitespace. A call to the .split() method on that string, with no arguments, returns a list of the parts of the string that are surrounded by whitespace.

>>> line = "  1.4   8.6  -23.49   "
>>> line.split()
['1.4', '8.6', '-23.49']

You can also specify a delimiter as the argument of the .split() method. Examples:

>>> s = 'farcical/aquatic/ceremony'
>>> s.split('/')
['farcical', 'aquatic', 'ceremony']
>>> "//a/b/".split('/')
['', '', 'a', 'b', '']
>>> "Stilton; Wensleydale; Cheddar;Edam".split("; ")
['Stilton', 'Wensleydale', 'Cheddar;Edam']

You may also provide a second argument that limits the number of pieces to be split from the string.

>>> t = 'a/b/c/d/e'
>>> t.split('/')
['a', 'b', 'c', 'd', 'e']
>>> t.split('/', 1)
['a', 'b/c/d/e']
>>> t.split('/', 3)
['a', 'b', 'c', 'd/e']