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. The general form is:

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 extra space character before the old value.

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'

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:

>>> 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'

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.

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

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”.

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'

There are string methods for testing what kinds of characters are in a string. Each of these methods is a predicate, that is, it asks a question and returns a value of True or False.

>>> mixed = 'abcDEFghi'
>>> mixed.isalpha()
True
>>> mixed.isupper()
False
>>> mixed.islower()
False
>>> "ABCDGOLDFISH".isupper()
True
>>> "lmno goldfish".islower()
True
>>> paradise = "87801"
>>> paradise.isalpha()
False
>>> paradise.isdigit()
True
>>> "abc123".isdigit()
False