1. Computing

Python String Methods - 2 of 2

By

  • stringObject.replace(old, new[, count]): Returns a copy of the string with all occurrences of substring old replaced by substitute new. The optional argument count indicates the maximum number of times for the replacement to be affected.
  • stringObject.rfind(sub [,start [,end]]): Returns the index of the last place in the string where the substring sub is found. The field to be searched may be restricted by using the optional arguments start and end. Optional arguments start and end are interpreted as in slice notation. On failure, this method returns "-1".
  • stringObject.rindex(sub[, start[, end]]): Like rfind(), but this method raises a ValueError when the substring sub is not found.
  • stringObject.rjust(width[, fillchar]): Returns the string right justified in a string of length width. As with ljust(), padding is done using the specified fillchar; if undeclared or left blank, the fillchar defaults to a space). The original string is returned if width is less than len(stringObject).
  • stringObject.rpartition(sep): Split the string at the last occurrence of sep, and return a 3-tuple containing the part before the separator, the separator itself, and the part after the separator. If the separator is not found, return a 3-tuple containing two empty strings, followed by the string itself. New in version 2.5.
  • stringObject.rsplit([sep [,maxsplit]]): Returns a list of the words in the string using sep as the delimiter. It does this by processing from the right and splitting on whitespace. If the optional arguement maxsplit is used, it indicates the maximum number of splits that should be done. If the delimiter sep is not specified or "None" is used, any whitespace string is considered a delimiter. Except for splitting from the right, rsplit() behaves much like split().
  • stringObject.rstrip([chars]): Returns a copy of the string with trailing characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a suffix; rather, all combinations of its values are stripped:
     >>> ' big string '.rstrip() 
     ' big string' 
     >>> 'Doremifasolatido'.rstrip('dmo') 
     'Doremifasolati' 
     >>> 'Doremifasolatido'.rstrip('Dmo') 
     'Doremifasolatid' 
     
  • stringObject.split([sep [,maxsplit]]): Returns a list of the words in the string using sep as the delimiter string. If the option maxsplit is given, at most that number of splits are done. The list will consequently have at most maxsplit+1 parts. Note: If the maxsplit option is not specified, then all possible splits are made. If the delimiter sep is not specified or is "None", several different splitting algorithms are applied. First, whitespace characters (spaces, tabs, newlines, returns, and formfeeds) are stripped from both ends. Then, words are separated by arbitrary length strings of whitespace characters. Consecutive whitespace delimiters are treated as a single delimiter.
  • stringObject.splitlines([keepends]): Returns 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.
  • stringObject.startswith(prefix[, start[, end]]): Returns "True" if the string starts with the given prefix, otherwise returns "False". "prefix" can also be a tuple of matches to find. Start and end delineate the beginning and end points within the string for the matching process.
  • stringObject.strip([chars]): Returns a copy of the string with the leading and trailing characters removed. When the chars argument is used, it represents a set of characters to be removed. If omitted or "None", the chars argument indicates that whitespace is to be removed by default. Once again, the chars argument is not a prefix or suffix, all combinations of its values are stripped.
     >>> ' big string '.strip() 
     'big string' 
     >>> 'Doremifasolatido'.strip('dmo') 
     'Doremifasolati' 
     >>> 'Doremifasolatido'.strip('Dmo') 
     'remifasolatid' 
     
  • stringObject.swapcase(): Returns a copy of the string with uppercase characters converted to lowercase and vice versa.
  • stringObject.title(): Returns a titlecased version of the string. All words start with uppercase characters, and all remaining (cased) characters are lowercase.
  • stringObject.translate(table[, deletechars]): Returns 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 (i.e., cover an entire keymap). Note: For Unicode objects, the translate() method does not accept the optional deletechars argument. Instead, it returns a copy of the string where all characters have been mapped through the given translation table, mapping Unicode ordinals to Unicode ordinals, Unicode strings or "None". Unmapped characters are left untouched. Characters mapped to "None" are deleted.
  • stringObject.upper(): Returns a copy of the string converted to uppercase.
  • stringObject.zfill(width): Returns the numeric string left filled with zeros in a string of length width. The original string is returned if width is less than len(stringObject).

©2014 About.com. All rights reserved.