The string module contains a number of functions to process standard Python strings, as shown in Example 1-51.
Example 1-51. Using the string Module
File: string-example-1.py import string text = "Monty Python's Flying Circus" print "upper", "=>", string.upper(text) print "lower", "=>", string.lower(text) print "split", "=>", string.split(text) print "join", "=>", string.join(string.split(text), "+") print "replace", "=>", string.replace(text, "Python", "Java") print "find", "=>", string.find(text, "Python"), string.find(text, "Java") print "count", "=>", string.count(text, "n") upper => MONTY PYTHON'S FLYING CIRCUS lower => monty python's flying circus split => ['Monty', "Python's", 'Flying', 'Circus'] join => Monty+Python's+Flying+Circus replace => Monty Java's Flying Circus find => 6 -1 count => 3
In Python 1.5.2 and earlier, the string module uses functions from the strop implementation module where possible.
In Python 1.6 and later, most string operations are made available as string methods as well, as shown in Example 1-52. Many of the functions in the string module are simply wrapper functions that call the corresponding string method.
Example 1-52. Using string Methods Instead of string Module Functions
File: string-example-2.py text = "Monty Python's Flying Circus" print "upper", "=>", text.upper() print "lower", "=>", text.lower() print "split", "=>", text.split() print "join", "=>", "+".join(text.split()) print "replace", "=>", text.replace("Python", "Perl") print "find", "=>", text.find("Python"), text.find("Perl") print "count", "=>", text.count("n") upper => MONTY PYTHON'S FLYING CIRCUS lower => monty python's flying circus split => ['Monty', "Python's", 'Flying', 'Circus'] join => Monty+Python's+Flying+Circus replace => Monty Perl's Flying Circus find => 6 -1 count => 3
In addition to the string-manipulation capabilities offered by string, the module also contains a number of functions that convert strings to other types (as Example 1-53 demonstrates).
Example 1-53. Using the string Module to Convert Strings to Numbers
File: string-example-3.py import string print int("4711"), print string.atoi("4711"), print string.atoi("11147", 8), # octal print string.atoi("1267", 16), # hexadecimal print string.atoi("3mv", 36) # whatever... print string.atoi("4711", 0), print string.atoi("04711", 0), print string.atoi("0x4711", 0) print float("4711"), print string.atof("1"), print string.atof("1.23e5") 4711 4711 4711 4711 4711 4711 2505 18193 4711.0 1.0 123000.0
In most cases (especially if you're using 1.6 or later), you can use the int and float functions instead of their string module counterparts.
The atoi function takes an optional second argument, which specifices the number base. If the base is 0, the function looks at the first few characters before attempting to interpret the value: if "0x," the base is set to 16 (hexadecimal), and if "0," the base is set to 8 (octal). The default is base 10 (decimal), just as if you hadn't provided an extra argument.
In 1.6 and later, int also accepts a second argument, just like atoi. Unlike the string versions, int and float accept Unicode strings.
Core Modules
More Standard Modules
Threads and Processes
Data Representation
File Formats
Mail and News Message Processing
Network Protocols
Internationalization
Multimedia Modules
Data Storage
Tools and Utilities
Platform-Specific Modules
Implementation Support Modules
Other Modules