Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

String Methods

A string object has a number of method functions. These can be grouped arbitrarily into transformations, which create new strings from old, and information, which returns a fact about a string. In all of the following method functions, we'll assume a string object named s .

The following string transformation functions create a new string from an existing string.

s. capitalize → string

Create a copy of the string s with only its first character capitalized.

s. center ( width ) → string

Create a copy of the string s centered in a string of length width . Padding is done using spaces.

s. encode ( encoding , [ errors ]) → string

Return an encoded string version of s . Default encoding is the current default string encoding. errors may be given to set a different error handling scheme. Default is 'strict' meaning that encoding errors raise a ValueError. Other possible values are 'ignore' and 'replace'.

s. expandtabs ( [tabsize] ) → string

Return a copy of s where all tab characters are expanded using spaces. If tabsize is not given, a tab size of 8 characters is assumed.

s. join ( sequence ) → string

Return a string which is the concatenation of the strings in the sequence . The separator between elements is s .

s. ljust ( width ) → string

Return s left justified in a string of length width . Padding is done using spaces.

s. lower → string

Return a copy of s converted to lowercase.

s. lstrip → string

Return a copy of s with leading whitespace removed.

s. replace ( old , new , [ maxsplit ]) → string

Return a copy of string s with all occurrences of substring old replaced by new . If the optional argument maxsplit is given, only the first maxsplit occurrences are replaced.

s. rjust ( width ) → string

Return s right justified in a string of length width . Padding is done using spaces.

s. rstrip → string

Return a copy of s with trailing whitespace removed.

s. strip → string

Return a copy of s with leading and trailing whitespace removed.

s. swapcase → string

Return a copy of s with uppercase characters converted to lowercase and vice versa.

s. title → string

Return a titlecased version of s , i.e. words start with uppercase characters, all remaining cased characters have lowercase.

s. translate ( table , [ deletechars ]) → string

Return a copy of the string s , 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. The translation tables are built using the string.maketrans function in the string module.

s. upper → string

Return a copy of s converted to uppercase.

The following accessor methods provide information about a string.

s. count ( sub , [ start , ] [ end ]) → int

Return the number of occurrences of substring sub in string s [ start : end ]. Optional arguments start and end are interpreted as in slice notation.

s. , (endswith suffix , [ start , ] [ end ]) → boolean

Return True if s ends with the specified suffix , otherwise return False. With optional start , or end , test s [ start : end ]. The suffix can be a single string or a sequence of individual strings.

s. , (find sub , [ start , ] [ end ]) → int

Return the lowest index in s 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.

s. , (index sub , [ start , ] [ end ]) → int

Like s. find but raise ValueError when the substring is not found.

s. , (isalnum) → boolean

Return True if all characters in s are alphanumeric and there is at least one character in s , False otherwise.

s. , (isalpha) → boolean

Return True if all characters in s are alphabetic and there is at least one character in s , False otherwise.

s. , (isdigit) → boolean

Return True if all characters in s are digits and there is at least one character in s , False otherwise.

s. , (islower) → boolean

Return True if all characters in s are lowercase and there is at least one cased character in s , False otherwise.

s. , (isspace) → boolean

Return True if all characters in s are whitespace and there is at least one character in s , False otherwise.

s. , (istitle) → boolean

Return True if s is a titlecased string, i.e. uppercase characters may only follow uncased characters and lowercase characters only cased ones, False otherwise.

s. , (isupper) → boolean

Return True if all characters in s are uppercase and there is at least one cased character in s , False otherwise.

s. , (rfind sub , [ start , ] [ end ]) → int

Return the highest index in s 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.

s. , (rindex sub , [ start , ] [ end ]) → int

Like s. rfind but raise ValueError when the substring is not found.

s. , (startswith prefix , [ start , ] [ end ]) → boolean

Return True if s starts with the specified prefix , otherwise return False. With optional start , or end , test s [ start : end ]. The prefix can be a single string or a sequence of individual strings.

The following generators create another kind of object, usually a sequence, from a string.

s. partition ( sep ) → 3-tuple

Return a three-tuple of the text prior to the first occurance of sep in the string s , the sep as the delimiter, and the text after the first occurance of the separator. If the separator doesn't occur, all of the input string is in the first element of the 3-tuple; the other two elements are empty strings.

s. split ( sep , [ maxsplit ]) → sequence

Return a list of the words in the string s , using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done. If sep is not specified, any whitespace string is a separator.

s. splitlines ( [keepends] ) → sequence

Return a list of the lines in s , breaking at line boundaries. Line breaks are not included in the resulting list unless keepends is given and true.

Here's another example of using some of the string methods and slicing operations.

for arg in sys.argv[1:]:
    argCap= arg.upper()
    if argCap[:2] == "-D":
        if argCap[2:] == "MYSQL":
            print "MySQL Connection" 
        elif argCap[2:] == "POSTGRES":
            print "Postgres Connection"
        elsif argCap[2:] == "SQLITE"
            print "SQLite Connection"
        else:
            print "'%s' is an unknown -D option" % argCap[2:]
    else:
        
parse other option types

            

We use the upper function to translate the provided parameter to upper case. This simplifies the comparison between the parameters. We take slices of each parameter string to compare the initial portion to see if it is -D, and we compare the final portion to a number of literal strings. Additionally, we use the formatting operation, % , to format an error report.


 
 
  Published under the terms of the Open Publication License Design by Interspire