You are on page 1of 2

Chapter 16.

Strings 129

Table 16.5. String Operations (1 of 2)

Method for any x Descriptiona


x.capitalize() "hello".capitalize()→ ’Hello’
x.center(n) Centers x in a string of length n.
"hello".center(15)→' hello '
x.count(sub) Counts number of times sub occurs in x
’mississippi’.count(’is’)→ 2
x.endswith(sub) Returns True if x ends with sub.
"Porcupine".endswith(’ine’)→True;
"Porcupine".endswith(’txt’)→False
x.expandtabs(n) Replaces tabs in x with n spaces.
x.find(sub) Returns the location of sub in the string x
"porcupine".find(’cup’) → 3
"porcupine".find(’fred’)→ -1
x.isalnum() True if all characters are alphanumeric.
"AbcD176".isalnum() → True
"Abc--176".isalnum() → False
x.isalpha() True if all characters are alphabetic.
"AbcD".isalpha() → True
"Abc176".isalpha() → False
x.isdigit() True if all characters are digits 0-9.
"01997".isdigit() → True
"Abc176".isdigit() → False
x.islower() True if all alphabetic characters are in lower case a to z.
"abc".islower() → True
"aBc".islower() → False
"abc-23".islower() → True
x.isspace() True if all characters are white space (e.g., blanks, tabs,
line feeds, etc. )
x.istitle() True if 1st letter each word is upper case.
"Hello World".istitle() → True
"hello world".istitle() → False
sep.join([x,..]) Joins the strings with separator sep
’-’.join([’good’,’morning’,’to’,’you’])
→ ’good-morning-to-you’
x.lstrip() Removes leading white space.
" fred ".lstrip() → "fred "
a For more detail see https://docs.python.org/3/library/index.html.

Scientic Computation: Python 3 Hacking for Math Junkies


130 Chapter 16. Strings

Table 16.5. String Operations (2 of 2)

Method for any x Example


x.partition(sep) Partitions a string into a sequence of strings at the first
occurrence of sep.
"sassafras".partition("a") →
(’s’, ’a’, ’ssafras’)
x.replace(from, to) Returns new string with substring replaced.
"sassafras".replace("as", "og") →
’sogsafrog’
x.rfind(sub) Finds sub in x, starting from the right.
"sogsafrog".rfind("o") → 7
x.rjust(n) Right justify x with length n
"frog".rjust(8) → " frog"
x.rpartition(sep) Like partitition but starts from the right.
"sassafras".rpartition("a") →
(’sassafr’, ’a’, ’as’)
x.rsplit(sep) Like split but starts from the right.
"sassafras".rsplit("a") →
[’s’, ’ss’, ’fr’, ’s’]
"sassafras".rsplit("a",2) →
[’sass’, ’fr’, ’s’]
x.rstrip() Removes trailing white spaces.
" frog " → " frog"
x.split(sep) Splits a string on a separator.
"sassafras".split("a")→
[’s’, ’ss’, ’fr’, ’s’]
"sassafras".split("a",1)→
[’s’, ’ssafras’].
x.startswith(sub) True if x starts with sub.
"sassafras".startswith("sas") → True
x.strip() Removes leading and trailing white space.
" frog ".strip()→"frog"
x.swapcase() Swaps upper and lower case.
"HelloWorld".swapcase()→’hELLOwORLD’
x.title() Puts first letter of each word upper case.
"good day".title()→"Good Day"
x.upper() Converts to upper case.
"Good Day".upper()→"GOOD DAY"

Scientic Computation: Python 3 Hacking for Math Junkies

You might also like