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 is an argument to
the method, just like an argument to a function.
argi
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.
Given some string value , to produce a new string containing s centered in a string
of length s, use
this method call:
n
s.center(n)
This method takes one argument , the size of the result.
Examples:
n
>>> 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.
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 , this method returns a new string
containing the characters from s with enough spaces added after it
to make a new string of length s.
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 . As with the
n.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 , use these methods to remove
leading and trailing whitespace:
s
returns s.strip()
with any leading or trailing whitespace characters
removed.
s
removes only leading whitespace.
s.lstrip()
removes only trailing whitespace.
s.rstrip()
>>> 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 searches
string s.count(t) to
see how many times string s occurs in it.
t
>>> 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 within a string
t:
s
s.find(t)
If matches
any part of t, the method returns the position where the first match
begins; otherwise, it returns -1.
s
>>> 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 , which returns the position
where the last match starts, or -1 if there is no match.
s.rfind(t)
>>> 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 starts with a string s using a method
call like this:
t
s.startswith(t)
This method returns True if starts with a
string that matches s; otherwise it returns tFalse.
>>> chiq
'banana'
>>> chiq.startswith("b")
True
>>> chiq.startswith("ban")
True
>>> chiq.startswith ( 'Waldo' )
False
There is a similar method that tests whether string s.endswith(t) ends with s:
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 and s.lower() are used to convert uppercase
characters to lowercase, and vice versa, respectively.
s.upper()
>>> poet = 'E. E. Cummings' >>> poet.upper() 'E. E. CUMMINGS' >>> poet.lower() 'e. e. cummings'
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.
tests whether all the characters of s.isalpha() are letters.
s
tests whether all the letters of s.isupper() are uppercase.
(It ignores any non-letter characters.)
s
tests whether all the letters of s.islower() are lowercase
letters. (This method also ignores non-letter
characters.)
s
tests whether all the characters of s.isdigit() are digits.
s
>>> 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
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']