• No results found

String Methods

In document Python Programming for Starters (Page 119-124)

Sequences: Strings, Lists, and Files

5.5 String Methods

5.5.1 Programming a Decoder

Now that we have a program to turn a message into a sequence of numbers, it would be nice if our friend on the other end had a similar program to turn the numbers back into a readable message.

Let’s solve that problem next. Our decoder program will prompt the user for a sequence of Unicode numbers and then print out the text message with the corresponding characters. This program presents us with a couple of challenges; we’ll address these as we go along.

The overall outline of the decoder program looks very similar to the encoder program. One change in structure is that the decoding version will collect the characters of the message in a string and print out the entire message at the end of the program. To do this, we need to use an accumulator variable, a pattern we saw in the factorial program from Chapter 3. Here is the decoding algorithm:

get the sequence of numbers to decode message = ""

for each number in the input:

convert the number to the corresponding Unicode character add the character to the end of message

print message

5.5. String Methods 111 Before the loop, the accumulator variable message is initialized to be an empty string, that is a string that contains no characters (""). Each time through the loop a number from the input is converted into an appropriate character and appended to the end of the message constructed so far.

The algorithm seems simple enough, but even the first step presents us with a problem. How exactly do we get the sequence of numbers to decode? We don’t even know how many numbers there will be. To solve this problem, we are going to rely on some more string manipulation operations.

First, we will read the entire sequence of numbers as a single string using input. Then we will split the big string into a sequence of smaller strings, each of which represents one of the numbers.

Finally, we can iterate through the list of smaller strings, convert each into a number, and use that number to produce the corresponding Unicode character. Here is the complete algorithm:

get the sequence of numbers as a string, inString split inString into a sequence of smaller strings message = ""

for each of the smaller strings:

change the string of digits into the number it represents append the Unicode character for that number to message print message

This looks complicated, but Python provides some functions that do just what we need.

You may have noticed all along that I’ve been talking about string objects. Remember from last chapter, objects have both data and operations (they “know stuff,” and “do stuff.”) By virtue of being objects, strings have some built-in methods in addition to the generic sequence operations that we have used so for. We’ll use some of those abilities here to solve our decoder problem.

For our decoder, we will make use of the split method. This method splits a string into a list of substrings. By default, it will split the string wherever a space occurs. Here’s an example:

>>> myString = "Hello, string methods!"

>>> myString.split()

[’Hello,’, ’string’, ’methods!’]

Naturally, the split operation is called using the usual dot notation for invoking one of an object’s methods. In the result, you can see how split has turned the original string "Hello, string methods!"into a list of three substrings: strings: "Hello,", "string", and "methods!".

By the way, split can be used to split a string at places other than spaces by supplying the character to split on as a parameter. For example, if we have a string of numbers separated by commas, we could split on the commas.

>>> "32,24,25,57".split(",") [’32’, ’24’, ’25’, ’57’]

Since our decoder program should accept the same format that was produced by the encoder program, namely a sequence of numbers with spaces between, the default version of split works nicely.

112 Chapter 5. Sequences: Strings, Lists, and Files

>>> "87 104 97 116 32 97 32 83 111 117 114 112 117 115 115 33".split() [’87’, ’104’, ’97’, ’116’, ’32’, ’97’, ’32’, ’83’, ’111’, ’117’,

’114’, ’112’, ’117’, ’115’, ’115’, ’33’]

Notice that the resulting list is not a list of numbers, it is a list of strings. It just so happens these strings contain only digits and could be interpreted as numbers.

All that we need now is a way of converting a string containing digits into a Python number.

Of course, we already know one way of doing that, we just need to evaluate the string with eval.

Recall, eval takes a string and evaluates it as if it were a Python expression. As a refresher, here are some interactive examples of eval:

>>> numStr = "500"

>>> eval(numStr) 500

>>> eval("345.67") 345.67

>>> eval("3+4") 7

>>> x = 3.5

>>> y = 4.7

>>> eval("x * y") 16.45

>>> x = eval(("Enter a number ")) Enter a number 3.14

>>> print x 3.14

Using split and eval we can write our decoder program.

# numbers2text.py

# A program to convert a sequence of Unicode numbers into

# a string of text.

def main():

print("This program converts a sequence of Unicode numbers into") print("the string of text that it represents.\n")

# Get the message to encode

inString = input("Please enter the Unicode-encoded message: ")

# Loop through each substring and build Unicode message message = ""

for numStr in inString.split():

codeNum = eval(numStr) # convert digits to a number

5.5. String Methods 113

message = message + chr(codeNum) # concatentate character to message print("\nThe decoded message is:", message)

main()

Study this program a bit, and you should be able to understand exactly how it accomplishes its task. The heart of the program is the loop.

for numStr in inString.split():

codeNum = eval(numStr)

message = message + chr(codeNum)

The split function produces a list of (sub)strings, and numStr takes on each successive string in the list. I called the loop variable numStr to emphasize that its value is a string of digits that represents some number. Each time through the loop, the next substring is converted to a number by evaling it. This number is converted to the corresponding Unicode character via chr and appended to the end of the accumulator, message. When the loop is finished, every number in inString has been processed and message contains the decoded text.

Here is an example of the program in action:

>>> import numbers2text

This program converts a sequence of Unicode numbers into the string of text that it represents.

Please enter the Unicode-encoded message:

83 116 114 105 110 103 115 32 97 114 101 32 70 117 110 33 The decoded message is: Strings are Fun!

5.5.2 More String Methods

Now we have a couple of programs that can encode and decode messages as sequences of Unicode values. These programs turned out to be quite simple due to the power both of Python’s string data type and its built-in sequence operations and string methods.

Python is a very good language for writing programs that manipulate textual data. Table 5.2 lists some other useful string methods. A good way to learn about these operations is to try them out interactively.

>>> s = "hello, I came here for an argument"

>>> s.capitalize()

’Hello, i came here for an argument’

>>> s.title()

’Hello, I Came Here For An Argument’

114 Chapter 5. Sequences: Strings, Lists, and Files

>>> s.lower()

’hello, i came here for an argument’

>>> s.upper()

’HELLO, I CAME HERE FOR AN ARGUMENT’

>>> s.replace("I", "you")

’hello, you came here for an argument’

>>> s.center(30)

’hello, I came here for an argument’

>>> s.center(50)

’ hello, I came here for an argument ’

>>> s.count(’e’) 5

>>> s.find(’,’) 5

>>> " ".join(["Number", "one,", "the", "Larch"])

’Number one, the Larch’

>>> "spam".join(["Number", "one,", "the", "Larch"])

’Numberspamone,spamthespamLarch’

I should mention that many of these functions, like split, accept additional parameters to customize their operation. Python also has a number of other standard libraries for text-processing that are not covered here. You can consult the online documentation or a Python reference to find out more.

Function Meaning

s.capitalize() Copy of s with only the first character capitalized s.center(width) Copy of s Centered s in a field of given width s.count(sub) Count the number of occurrences of sub in s s.find(sub) Find the first position where sub occurs in s

s.join(list) Concatenate list into a string, using s as separator s.ljust(width) Like center, but s is left-justified

s.lower() Copy of s in all lowercase characters

s.lstrip() Copy of s with leading white space removed s.replace(oldsub,newsub) Replace all occurrences of oldsub in s with newsub s.rfind(sub) Like find, but returns the rightmost position s.rjust(width) Like center, but s is right-justified

s.rstrip() Copy of s with trailing white space removed s.split() Split s into a list of substrings (see text)

s.title() Copy of s with first character of each word capitalized s.upper() Copy of s with all characters converted to upper case

Table 5.2: Some string methods

In document Python Programming for Starters (Page 119-124)