Python Programming for the Absolute Beginner, 3rd Edition
As you just learned, strings are one type of sequence, made up of individual characters. Python offers some useful functions and operators that work with any kind of sequence, including strings. These operators and functions can tell you basic but important things about a sequence, such as how long it is or whether a certain element is in it.
Introducing the Message Analyzer Program
This next program analyzes any message that you enter. It tells you how long the message is and whether or not it contains the most common letter in the English language (the letter "e"). The program accomplishes this with a new sequence function and sequence operator. Figure 4.4 shows off the program.
Here's the code for the program:
# Message Analyzer # Demonstrates the len() function and the in operator # Michael Dawson - 1/26/03 message = raw_input("Enter a message: ") print "\nThe length of your message is:", len(message) print "\nThe most common letter in the English language, 'e',", if "e" in message: print "is in your message." else: print "is not in your message." raw_input("\n\nPress the enter key to exit.")
Using the len() Function
After the program imports the random module and gets the user's message, it prints the message length with
print "\nThe length of your message is:", len(message)
You can give any sequence you want to len() and it will tell you that sequence's length. A sequence's length is the number of elements it has. Since message has 10 characters in it (you count every character, including the space and exclamation point), it has a length of 10, just like the computer told you.
Using the in Operator
The letter "e" is the most common letter in English. The program uses the following lines to test whether "e" is in the message the user entered:
print "\nThe most common letter in the English language, 'e',", if "e" in message: print "is in your message." else: print "is not in your message."
The condition in the if statement is "e" in message. If message contains the character "e", it's true. If message doesn't contain "e", it's false. In the sample run, the value of message is "Game Over!", which does contain the character "e". So, the condition "e" in message evaluated to true and the computer printed "is in your message." If the condition had been false (for example, if message had been equal to "Python Programming"), then the computer would have displayed is not in your message. If an element is in a sequence, it's said to be a member of the sequence.
You can use in anywhere in your own programs to check if an element is a member of sequence. Just put the element you want to check for, followed by in, followed by the sequence. This creates a condition. If the element is a member, the condition is true; otherwise it's false.
TRAP | The in operator can only check for a single element in a sequence. In the case of strings, that means it can only check for a single character. So, if "e" in message is a valid use of in, but if "Over" in message is not. Using in to test if more than one letter is in a string will get you an nice, juicy error. |
Категории