1. Computing

Python String Methods - 1 of 2

By

Python's built-in string methods are incredibly powerful. As the name implies, each of the following methods are available through class String. Every string object is an instance of that class and has these methods available. In the following summary, the string object is referenced as stringObject for pedagogical reasons. Where character cases are involved, the treatment of 8-bit strings is locale-dependent. This is the first of a two-page reference.

  • stringObject.capitalize(): Returns a copy of the string with its first character capitalised.
  • stringObject.center(width[, fillchar]): Returns the string centered according to the length of the string indicated as width. By default, padding is done using an empty space, but the exact character may be set by specifying the optional fillchar argument.
  • stringObject.count(sub[, start[, end]]): Returns the number of occurrences of substring sub in the string which begins at position start and ends at position end.
  • stringObject.decode([encoding[, errors]]): Decodes the string using the codec registered for encoding. If no encoding is declared, the default string encoding is used. If no error handling scheme is set with errors, Python uses 'strict', and encoding errors thus raise UnicodeErrors. Other error handling schemes are 'ignore', 'replace' and any other name registered via codecs.register_error (to find out what these are for your Python installation, import codec and call for a directory of the codecs.register_error function using 'dir(codecs.register_error)').
  • stringObject.encode([encoding[,errors]]): Returns an encoded version of the string. As with decode(), above, the default encoding is 'strict' and others are available. These include 'ignore', 'replace', 'xmlcharrefreplace', 'backslashreplace' and any other scheme listed in codecs.register_error (to find out what these are for your Python installation, import codec and call for a directory of the codecs.register_error function using 'dir(codecs.register_error)').
  • stringObject.endswith(suffix[, start[, end]]): Returns "True" if the string ends with suffix, otherwise it returns "False". The value of suffix may also be a tuple of suffixes to match. The optional start and end values denote the beginning and end points of the match within the string.
  • stringObject.expandtabs([tabsize]): Returns 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.
  • stringObject.find(sub[, start[, end]]): Returns the lowest index in the string where the substring sub is found within the slice range of start and end. Returns -1 if sub is not found.
  • stringObject.index(sub[, start[, end]]): Functions Like find() but raises ValueError if the substring is not found. Start and end positions are interpreted as in slice notation.
  • stringObject.isalnum(): Returns "true" if all characters in the string are alphanumeric and there is at least one character, false otherwise.
  • stringObject.isalpha(): Returns "true" if all characters in the string are alphabetic and there is at least one character, false otherwise.
  • stringObject.isdigit(): Returns true if all the characters in the string are digits. There must be at least one character, otherwise it returns "false".
  • stringObject.islower(): Returns true if all cased characters in the string are lowercase. There must be at least one cased character. It returns false otherwise.
  • stringObject.isspace(): Returns true if there are only whitespace characters in the string. There must be at least one character. It returns false otherwise.
  • stringObject.istitle(): Returns true if the string is a titlecased string and there is at least one character. For example, uppercase characters may only follow uncased characters (i.e., characters that do not have uppercase and lowercase forms). Lowercase characters may follow only cased ones. Returns "false" otherwise.
  • stringObject.isupper(): Tests whether all cased characters in the string are uppercase and requires that there be at least one cased character. Returns "true" if so and "false" otherwise.
  • stringObject.join(seq): Returns a string which is the concatenation of the strings in the sequence seq. The delimiter of the sequence is the string providing this method.
  • stringObject.ljust(width[, fillchar]): Returns the string left justified in a string of length width. Padding is done using the specified fillchar (default is a space). The original string is returned if width is less than len(stringObject).
  • stringObject.lower(): Returns a copy of the string converted to lowercase.
  • stringObject.lstrip([chars]): Returns a copy of the string with leading characters removed. One can use the optional chars argument to specify a set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a prefix; rather, all combinations of its values are stripped when they lead the string:
     >>> ' big string '.lstrip() 
     'big string ' 
     >>> 
     >>> 'Doremifasolatido'.lstrip('dmo') 
     'Doremifasolatido' 
     >>> 
     >>> 'Doremifasolatido'.lstrip('Dmo') 
     'remifasolatido' 
     
  • stringObject.partition(sep): Splits the string at the first occurrence of delimiter sep, and returns a tripartite tuple that contains the part before the separator, the separator itself, and the part after the separator. If the separator is not found, this method returns a tripartite tuple that contains the string itself, followed by two empty strings. This method first shipped with Python 2.5.
  1. About.com
  2. Computing
  3. Python
  4. Python Library
  5. Python String Methods: Built-in Methods for Handling Strings in Python

©2014 About.com. All rights reserved.