Building a New String


You've already seen how you can concatenate two strings with the + operator. Sometimes, you may want to build a new string, one character at a time. Since strings are immutable, what you'll really be doing is creating a new string every time you use the concatenation operator.

Introducing the No Vowels Program

This next program, No Vowels, takes a message from the user and prints it, minus any vowels. Figure 4.8 shows the program in action.

click to expand
Figure 4.8: Using a for loop, new strings are created. The program skips the concatenation operation for any vowels.

The program creates a new string of the original message, without the vowels. Really what it does is create a series of new strings. Here's the code:

 # No Vowels # Demonstrates creating new strings with a for loop # Michael Dawson - 1/27/03 message = raw_input("Enter a message: ") new_message = "" VOWELS = "aeiou" print for letter in message:     if letter.lower() not in VOWELS:         new_message += letter         print "A new string has been created:", new_message print "\nYour message without vowels is:", new_message raw_input("\n\nPress the enter key to exit.") 

Creating Constants

After the program gets the message from the user and creates an empty new message, it creates a string:

 VOWELS = "aeiou" 

This variable, VOWELS, is assigned a string of all the vowels. You probably notice that the variable name is in all caps, contrary to what you have learned: that, traditionally, variable names are in lowercase. Well, I haven't veered from tradition here. In fact, there's a special meaning associated with variable names in all caps. They're called constants and refer to a value that is not meant to change (their value is constant).

Constants are valuable to programmers in two ways. First, they make programs clearer. In this program, I can use the variable name VOWELS anywhere I need the sequence of vowels, instead of the string "aeiou". Using the variable name instead of the string is clearer. When you see the variable name, you understand what it means, but you might be confused by seeing the odd-looking string itself. Second, constants save retyping (and possibly errors from mistyping). Constants are especially useful if you have a long value, like a very long number or string. Use a constant in programs where you have the same, unchanging value used in multiple places.

TRAP

You have to be careful when you create constants by making an all-caps variable name. Even though you're saying to yourself and other programmers that this variable will always refer to the same value, there's nothing in Python that will stop you from changing it in your program. This naming practice is simply a convention. So, once you create a variable with a name in all caps, make sure to treat it as unchangeable.

start sidebar
IN THE REAL WORLD

In some programming languages, constants are exactly that. They can't be changed once they're defined. That's the safest way to create and use constants. In Python, though, there isn't a simple way to create true constants of your own.

end sidebar

Creating New Strings from Existing Ones

The real work of the program happens in the loop. The program creates a new message, without any vowels, as the loop runs. Each time through, the computer checks the next letter in the original message. If it's not a vowel, it adds this letter to the new message it's creating. If it is a vowel, the program moves on to the next letter. You know that a program can't literally add a character to a string, so, more precisely, when the program comes across a character that's not a vowel, it concatenates the new message it has so far with this character. The code that accomplishes this is:

 for letter in message:     if letter.lower() not in VOWELS:         new_message += letter         print "A new string has been created:", new_message 

There are two new ideas in the loop, so let me go over both of them. First, Python is picky when dealing with strings and characters. "A" is not the same as "a". Since VOWELS is assigned a string that contains only lowercase letters, I needed to make sure that I checked only lowercase letters when using the in operator. That's why I used letter.lower().

TRICK

Often, when you compare two strings, you don't care about the case matching, only the letters. If you ask a player if he or she wants to continue a game, the string "Yes" is as good as the string "yes". Well, in these instances, just make sure to convert both strings to the same case (upper- or lowercase, it doesn't matter) before you compare them.

Here's an example. Let's say I want to compare two strings, name and winner, to see if they are equal, and I don't care about matching the case. I could create the condition:

 name.lower() == winner.lower() 

This condition is true whenever name and winner each have the same sequence of characters, regardless of case. So, "Larry" and "larry" is a match. "LARRY" and "larry" is too. Even "LaRrY" and "lArRy" works.

Second, you also might notice that I used the augmented assignment operator, +=, in the program for string concatenation. You saw the augmented assignment operators with numbers, but they also work with strings. So, this line:

        new_message += letter 

is exactly the same as

    new_message = new_message + letter 




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