Built-in class str
Official Python documentation
Purpose
- Supports the manipulation of character sequences. Built-in; i.e., does not require import for its usage.
`
Remember
- Python strings are immutable; that is, there is no way to change an existing string. However, there are plenty of ways to create new strings out of existing strings.
Suppose
a = 'abcdefghijklm'
b = 'Abra-Ca-Dabra!!'
c = 'ra'
d = '314159'
e = '3.1415'
f = 'cs 1112'
g = ' cs 1112 '
Built-in function and operators
len( s )
- Returns the length of the sequence
s
.
- Sample usage:
len( a )
str( n )
- Returns string representation of
n
.
- Sample usage:
str( 12345 )
Returns
'12345'
.
- Sample usage:
str( 12.345 )
Returns
'12.345'
.
[ i : j ]
- Returns a substring (slice) of its string. The substring is the string's characters at indices
i
to indexj-1
. Sample usage:a[ 2 : 6 ]
returns'cdef'
. They are the characters at indices2
through5
in'abcdefghijklm'
.
[ i : ]
- Returns the string's characters at from index
i
on. Sample usage:a[ 2 : ]
returns'cdefghijklm'
. They are the characters at indices0
through5
in'abcdefghijklm'
.
[ : j ]
- Returns the string's characters at indices
0
toj-1
. Sample usage:a[ : 6 ]
returns'abcdef'
. They are the characters at indices0
through5
in'abcdefghijklm'
.
[ i ]
- Returns the character at index
i
. Sample usage:a[ 2 ]
returns'c'
. The character at index6
in'abcdefghijklm'
.
[ : ]
- Returns the string's characters at indices
0
ton-1
, wheren
is the length of the string; that is, returns a copy of the string. Sample usage:a[ : ]
returns'abcdefghijklm'
. They are the characters at indices0
through5
in'abcdefghijklm'
.
Essential member methods
capitalize()
- Returns a new copy of its string with first character capitalized and the other characters in lowercase. Sample usage:
a.capitalize()
returns'Abcdefghijklm'
.
count( s )
- Returns the number of occurrences of
s
in its string. Sample usage:b.count( c )
returns 2. There are two occurrences of'ra'
in'Abra-Ca-Dabra!!'
.
count( s, i, j )
- Returns the number of occurrences of
s
in its string in index interval [i
,j
). Sample usage:b.count( c, 4, 12 )
returns 1. There is one occurrence of'ra'
in'Abra-Ca-Dabra!!'
in index interval [ 4, 12 )
find( s )
find( s, i )
- Returns the index of the first occurrence of
s
givens
is in string slice[ i : n ]
, wheren
is the length of the string. If there are no occurrences, returns-1
. Sample usage:a.find( c, 3 )
returns-1
. There are no occurrences of'ra'
in'abcdefghijklm'
. Sample usage:b.find( c, 3 )
returns11
. First occurrence of'ra'
in'Abra-Ca-Dabra!!'
after index3
is at index11
.
find( s, i, j )
- Returns the first occurrence of
s
givens
is in string slice[ i : j ]
. If there are no occurrences, returns-1
. Sample usage:b.find( c, 4, 14 )
returns 11. The first occurrence of'ra'
in'Abra-Ca-Dabra!!'
within the indicated section is at index11
.
format( v1, v2, ... )
- Returns a formatted version of the
v
values. See the writeup about field formatting.
isalpha()
- If its string is empty, it returns
False
; Otherwise, returns whether all characters in its string are alphabetic.
- Sample usage:
a.isalpha()
Returns
True
as all characters in'abcdefghijklm'
are alphabetic.
- Sample usage:
b.isalpha()
Returns
False
as not all characters in'Abra-Ca-Dabra!!'
are alphabetic.
isdigit()
- If its string is empty, it returns
False
; Otherwise, returns whether all characters in its string are base 10 digits.
- Sample usage:
d.isdigit()
Returns
True
as all characters in'314159'
are digits.
- Sample usage:
e.isdigit()
Returns
False
not all characters in'3.1415'
are digits.
lower()
- Returns a new copy of its string with all characters converted to lowercase
- Sample usage:
b.lower()
Returns
abra-ca-dabra!!
.
replace( s, t, n )
- Parameter
n
is optional. Ifn
is not there, it returns a new copy of its string with all occurrences ofs
replaced byt
. Ifn
is given, it returns a new copy with the firstn
occurrences ofs
replaced byt
.
- Sample usage:
r.replace( 'a', '___' )
Returns
'Abr___-C___-D___br___!!'
. All occurrences of'a'
in'Abra-Ca-Dabra!!'
are replaced with'___'
. Note,'A'
is not changed.
- Sample usage:
b.replace( 'a', '___', 2 )
Returns
'Abr___-C___-Dabra!!'
. The first two occurrences of'a'
in'Abra-Ca-Dabra!!'
are replaced with'___'
.
rfind( s )
- Returns the index of the last occurrence of
s
in its string. If there are no occurrences, returns-1
.
- Sample usage:
b.rfind( c )
Returns
11
. Last occurrence of'ra'
in'Abra-Ca-Dabra!!'
is at index11
.
rfind( s, i, j )
- Returns the last occurrence of
s
in its string in index interval [i
,j
). If there are no occurrences, returns-1
.
- Sample usage:
b.rfind( c, 1, 10 )
Returns 2. The last occurrence of
'ra'
in'Abra-Ca-Dabra!!'
in index interval [ 1, 14 ) is2
.
split()
- Returns a list of words in its string. See the next method listed here when an optional parameter is given.
- Sample usage:
f.split()
Returns the list
[ 'cs', '1112' ]
.
split( d )
- Returns a list of parts in its string. Removes any occurrences of the parameter
d
to determine how many parts to create
- Sample usage:
b.split( 'a' )
Returns the list
[ 'Abr', '-C', '-D', 'br', '!!' ]
.
strip()
- Returns a new string without any leading or trailing white-space (tabs, spaces, or newlines). If given a parameter, removes those characters instead.
- Sample usage:
g.strip()
Returns the string
'cs 1112'
.
- Sample usage:
a.strip( 'bam' )
Returns the string
'cdefghijkl'
.
upper()
- Returns a new copy of its string with all characters converted to uppercase.
- Sample usage:
b.upper()
Returns
ABRA-CA-DABRA!!
.