Using String Methods


Python has a rich set of tools for working with strings. One type of these tools is string methods. String methods allow you to create new strings from old ones. You can do everything from the simple, such as create a string that's just an all-capital-letters version of the original, to the complex, such as create a new string that's the result of a series of intricate letter substitutions.

Introducing the Quotation Manipulation Program

According to Mark Twain, "The art of prophecy is very difficult, especially with respect to the future." No one can accurately foretell the future, but it's still amusing to read predictions that pundits have made about technology. A good one is, "I think there is a world market for maybe five computers." This was made by then IBM chairman, Thomas Watson, in 1943. The Quotation Manipulation program that I wrote prints this quote several ways using string methods. (Fortunately, I was able to write this program because I happen to own computer #3.) Take a look at the sample run in Figure 2.8.

click to expand
Figure 2.8: This slightly low guess is printed several ways with the help of string methods.

The following is the code for the program:

 # Quotation Manipulation # Demonstrates string methods # Michael Dawson 1/11/03 # quote from IBM Chairman, Thomas Watson, in 1943 quote = "I think there is a world market for maybe five computers." print "Original quote:" print quote print "\nIn uppercase:" print quote.upper() print "\nIn lowercase:" print quote.lower() print "\nAs a title:" print quote.title() print "\nWith a minor replacement:" print quote.replace("five", "millions of") print "\nOriginal quote is still:" print quote raw_input("\n\nPress the enter key to exit.") 

Creating New Strings with String Methods

Though there's a new concept at work here, the code is still pretty understandable. Take a look at the line:

 print quote.upper() 

You can probably guess what it does: print a version of quote in all uppercase letters.

The line does this through the use of a string method, upper(). A string method is like an ability a string has. So, quote has the ability to create a new string, a capitalized version of itself, through its upper() method. When it does this, it returns this new string, and the line becomes equivalent to the following line:

 print "I THINK THERE IS A WORLD MARKET FOR MAYBE FIVE COMPUTERS." 

Now, the line of code is never like this, but you can think of it in this way to help you understand how the method works.

You've probably noticed the parentheses in this method call. It should remind you of functions, which you just learned about in this chapter. Methods are similar to functions. The main difference is that a built-in function, like raw_input(), can be called on its own. But a string method has to be called through a particular string. It makes no sense to just type the following:

 print upper() 

You kick off a method, or invokeit, by adding a dot, followed by the name of the method, followed by a pair of parentheses, after a string value. The parentheses aren't just for show. Just as with functions, you can pass arguments inside them. upper() doesn't take any arguments, but you'll see an example of a string method that does with replace().

The line

 print quote.lower() 

invokes the lower() method of quote to create an all-lowercase-letters version, which it returns. Then, that new, lowercase string is printed.

The line

 print quote.title() 

prints a version of quote that's like a title. The title() method returns a string where the first letter of each word is capitalized and the rest of the string is in lowercase.

The line

 print quote.replace("five", "millions of") 

prints a new string, where every occurrence of "five" in quote are replaced with "millions of".

The method replace() needs at least two pieces of information: the old text to be replaced, and the new text that replaces it. You separate the two arguments with a comma. You can add an optional third argument, an integer, that tells the method the maximum number of times to make the replacement.

Finally, the program prints quote again, with

 print "\nOriginal quote is still:" print quote 

You can see from Figure 2.8 that quote hasn't changed. Remember, string methods create a new string. They don't affect the original one. Table 2.4 summarizes the string methods you've just seen, along with a few others.

Table 2.4: USEFUL STRING METHODS

Method

Description

upper()

Returns the uppercase version of the string.

lower()

Returns the lowercase version of the string.

swapcase()

Returns a new string where the case of each letter is switched. Uppercase becomes lowercase and lowercase becomes uppercase.

capitalize()

Returns a new string where the first letter is capitalized and the rest are lowercase.

title()

Returns a new string where the first letter of each word is capitalized and all others are lowercase.

strip()

Returns a string where all the white space (tabs, spaces, and newlines) at the beginning and end is removed.

replace(old, new [,max])

Returns a new string where occurrences of the string old are replaced with the string new. The optional max limits the number of replacements.




Python Programming for the Absolute Beginner
Python Programming for the Absolute Beginner, 3rd Edition
ISBN: 1435455002
EAN: 2147483647
Year: 2003
Pages: 194

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net