2.3.6.1 String Methods

These are the string methods which both 8-bit strings and Unicode objects support:

capitalize( )
Return a copy of the string with only its first character capitalized.

center( width)
Return centered in a string of length width. Padding is done using spaces.

count( sub[, start[, end]])
Return the number of occurrences of substring sub in string S[start:end]. Optional arguments start and end are interpreted as in slice notation.

decode( [encoding[, errors]])
Decodes the string using the codec registered for encoding. encoding defaults to the default string encoding. errors may be given to set a different error handling scheme. The default is 'strict', meaning that encoding errors raise ValueError. Other possible values are 'ignore' and 'replace'. New in version 2.2.

encode( [encoding[,errors]])
Return an encoded version of the string. Default encoding is the current default string encoding. errors may be given to set a different error handling scheme. The default for errors is 'strict', meaning that encoding errors raise a ValueError. Other possible values are 'ignore' and 'replace'. New in version 2.0.

endswith( suffix[, start[, end]])
Return True if the string ends with the specified suffix, otherwise return False. With optional start, test beginning at that position. With optional end, stop comparing at that position.

expandtabs( [tabsize])
Return a copy of the string where all tab characters are expanded using spaces. If tabsize is not given, a tab size of 8 characters is assumed.

find( sub[, start[, end]])
Return the lowest index in the string where substring sub is found, such that sub is contained in the range [start, end). Optional arguments start and end are interpreted as in slice notation. Return -1 if sub is not found.

index( sub[, start[, end]])
Like find(), but raise ValueError when the substring is not found.

isalnum( )
Return true if all characters in the string are alphanumeric and there is at least one character, false otherwise.

isalpha( )
Return true if all characters in the string are alphabetic and there is at least one character, false otherwise.

isdigit( )
Return true if all characters in the string are digits and there is at least one character, false otherwise.

islower( )
Return true if all cased characters in the string are lowercase and there is at least one cased character, false otherwise.

isspace( )
Return true if there are only whitespace characters in the string and there is at least one character, false otherwise.

istitle( )
Return true if the string is a titlecased string and there is at least one character, i.e. uppercase characters may only follow uncased characters and lowercase characters only cased ones. Return false otherwise.

isupper( )
Return true if all cased characters in the string are uppercase and there is at least one cased character, false otherwise.

join( seq)
Return a string which is the concatenation of the strings in the sequence seq. The separator between elements is the string providing this method.

ljust( width)
Return the string left justified in a string of length width. Padding is done using spaces. The original string is returned if width is less than len(s).

lower( )
Return a copy of the string converted to lowercase.

lstrip( [chars])
Return a copy of the string with leading characters removed. If chars is omitted or None, whitespace characters are removed. If given and not None, chars must be a string; the characters in the string will be stripped from the beginning of the string this method is called on. Changed in version 2.2.2: Support for the chars argument.

replace( old, new[, count])
Return a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.

rfind( sub [,start [,end]])
Return the highest index in the string where substring sub is found, such that sub is contained within s[start,end]. Optional arguments start and end are interpreted as in slice notation. Return -1 on failure.

rindex( sub[, start[, end]])
Like rfind() but raises ValueError when the substring sub is not found.

rjust( width)
Return the string right justified in a string of length width. Padding is done using spaces. The original string is returned if width is less than len(s).

rstrip( [chars])
Return a copy of the string with trailing characters removed. If chars is omitted or None, whitespace characters are removed. If given and not None, chars must be a string; the characters in the string will be stripped from the end of the string this method is called on. Changed in version 2.2.2: Support for the chars argument.

split( [sep [,maxsplit]])
Return a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done. If sep is not specified or None, any whitespace string is a separator.

splitlines( [keepends])
Return a list of the lines in the string, breaking at line boundaries. Line breaks are not included in the resulting list unless keepends is given and true.

startswith( prefix[, start[, end]])
Return True if string starts with the prefix, otherwise return False. With optional start, test string beginning at that position. With optional end, stop comparing string at that position.

strip( [chars])
Return a copy of the string with leading and trailing characters removed. If chars is omitted or None, whitespace characters are removed. If given and not None, chars must be a string; the characters in the string will be stripped from the both ends of the string this method is called on. Changed in version 2.2.2: Support for the chars argument.

swapcase( )
Return a copy of the string with uppercase characters converted to lowercase and vice versa.

title( )
Return a titlecased version of the string: words start with uppercase characters, all remaining cased characters are lowercase.

translate( table[, deletechars])
Return a copy of the string where all characters occurring in the optional argument deletechars are removed, and the remaining characters have been mapped through the given translation table, which must be a string of length 256.

For Unicode objects, the translate() method does not accept the optional deletechars argument. Instead, it returns a copy of the s where all characters have been mapped through the given translation table which must be a mapping of Unicode ordinals to Unicode ordinals, Unicode strings or None. Unmapped characters are left untouched. Characters mapped to None are deleted. Note, a more flexible approach is to create a custom character mapping codec using the codecs module (see encodings.cp1251 for an example).

upper( )
Return a copy of the string converted to uppercase.

zfill( width)
Return the numeric string left filled with zeros in a string of length width. The original string is returned if width is less than len(s). New in version 2.2.2.

See About this document... for information on suggesting changes.