Beginning Python - From Novice to Professional
Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional
CHAPTER 3 ■ WORKING WITH STRINGS 63 split A very important string method, split is the inverse of join, and is used to split a string into a sequence: >>> '1+2+3+4+5'.split('+') ['1', '2', '3', '4', '5'] >>> '/usr/bin/env'.split('/') ['', 'usr', 'bin', 'env'] >>> 'Using the default'.split() ['Using', 'the', 'default'] Note that if no separator is supplied, the default is to split on all runs of consecutive whitespace characters (spaces, tabs, newlines, and so on). See also: join. In Appendix B: rsplit, splitlines. strip The strip method returns a string where whitespace on the left and right (but not internally) has been stripped (removed): >>> ' internal whitespace is kept '.strip() 'internal whitespace is kept' As with lower, strip can be useful when comparing input to stored values. Let’s return to the user name example from the section on lower, and let’s say that the user inadvertently types a space after his name: >>> names = ['gumby', 'smith', 'jones'] >>> name = 'gumby ' >>> if name in names: print 'Found it!' ... >>> if name.strip() in names: print 'Found it!' ... Found it! >>> You can also specify which characters are to be stripped, by listing them all in a string parameter: >>> '*** SPAM * for * everyone!!! ***'.strip(' *!') 'SPAM * for * everyone' Stripping is only performed at the ends, so the internal asterisks are not removed. In Appendix B: lstrip, rstrip.
64 CHAPTER 3 ■ WORKING WITH STRINGS translate Similar to replace, translate replaces parts of a string, but unlike replace, translate only works with single characters. Its strength lies in that it can perform several replacements simultaneously, and can do so more efficiently than replace. There are quite a few rather technical uses for this method (such as translating newline characters or other platform-dependent special characters), but let’s consider a simpler (although slightly more silly) example. Let’s say you want to translate a plain English text into one with a German accent. To do this, you must replace the character “c” with “k,” and “s” with “z.” Before you can use translate, however, you must make a translation table. This translation table is a full listing of which characters should be replaced by which. Because this table (which is actually just a string) has 256 entries, you won’t write it out yourself: You’ll use the function maketrans from the string module. The maketrans function takes two arguments: two strings of equal length, indicating that each character in the first string should be replaced by the character in the same position in the second string. Got that? In the case of our simple example, the code would look like the following: >>> from string import maketrans >>> table = maketrans('cs', 'kz') WHAT’S IN A TRANSLATION TABLE? A translation table is a string containing one replacement letter for each of the 256 characters in the ASCII character set: >>> table = maketrans('cs', 'kz') >>> len(table) 256 >>> table[97:123] 'abkdefghijklmnopqrztuvwxyz' >>> maketrans('', '')[97:123] 'abcdefghijklmnopqrstuvwxyz' As you can see, I’ve sliced out the part of the table that corresponds to the lowercase letters. Take a look at the alphabet in the table and that in the empty translation (which doesn’t change anything). The empty translation has a normal alphabet, while in the preceding code, the letter “c” has been replaced by “k,” and “s” has been replaced by “z.” Once you have this table, you can use it as an argument to the translate method, thereby translating your string: >>> 'this is an incredible test'.translate(table) 'thiz iz an inkredible tezt'
- Page 44 and 45: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 46 and 47: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 48 and 49: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 50 and 51: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 52 and 53: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 54 and 55: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 56 and 57: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 58 and 59: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 60 and 61: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 62 and 63: CHAPTER 2 ■ ■ ■ Lists and Tup
- Page 64 and 65: CHAPTER 2 ■ LISTS AND TUPLES 33 I
- Page 66 and 67: CHAPTER 2 ■ LISTS AND TUPLES 35 A
- Page 68 and 69: CHAPTER 2 ■ LISTS AND TUPLES 37 >
- Page 70 and 71: CHAPTER 2 ■ LISTS AND TUPLES 39 M
- Page 72 and 73: CHAPTER 2 ■ LISTS AND TUPLES 41 T
- Page 74 and 75: CHAPTER 2 ■ LISTS AND TUPLES 43 L
- Page 76 and 77: CHAPTER 2 ■ LISTS AND TUPLES 45 W
- Page 78 and 79: CHAPTER 2 ■ LISTS AND TUPLES 47
- Page 80 and 81: CHAPTER 2 ■ LISTS AND TUPLES 49 a
- Page 82 and 83: CHAPTER 2 ■ LISTS AND TUPLES 51 S
- Page 84 and 85: CHAPTER 3 ■ ■ ■ Working with
- Page 86 and 87: CHAPTER 3 ■ WORKING WITH STRINGS
- Page 88 and 89: CHAPTER 3 ■ WORKING WITH STRINGS
- Page 90 and 91: CHAPTER 3 ■ WORKING WITH STRINGS
- Page 92 and 93: CHAPTER 3 ■ WORKING WITH STRINGS
- Page 96 and 97: CHAPTER 3 ■ WORKING WITH STRINGS
- Page 98 and 99: CHAPTER 4 ■ ■ ■ Dictionaries:
- Page 100 and 101: CHAPTER 4 ■ DICTIONARIES: WHEN IN
- Page 102 and 103: CHAPTER 4 ■ DICTIONARIES: WHEN IN
- Page 104 and 105: CHAPTER 4 ■ DICTIONARIES: WHEN IN
- Page 106 and 107: CHAPTER 4 ■ DICTIONARIES: WHEN IN
- Page 108 and 109: CHAPTER 4 ■ DICTIONARIES: WHEN IN
- Page 110: CHAPTER 4 ■ DICTIONARIES: WHEN IN
- Page 113 and 114: 82 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 115 and 116: 84 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 117 and 118: 86 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 119 and 120: 88 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 121 and 122: 90 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 123 and 124: 92 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 125 and 126: 94 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 127 and 128: 96 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 129 and 130: 98 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 131 and 132: 100 CHAPTER 5 ■ CONDITIONALS, LOO
- Page 133 and 134: 102 CHAPTER 5 ■ CONDITIONALS, LOO
- Page 135 and 136: 104 CHAPTER 5 ■ CONDITIONALS, LOO
- Page 137 and 138: 106 CHAPTER 5 ■ CONDITIONALS, LOO
- Page 139 and 140: 108 CHAPTER 5 ■ CONDITIONALS, LOO
- Page 141 and 142: 110 CHAPTER 6 ■ ABSTRACTION But w
- Page 143 and 144: 112 CHAPTER 6 ■ ABSTRACTION ■Ti
64 CHAPTER 3 ■ WORKING WITH STRINGS<br />
translate<br />
Similar <strong>to</strong> replace, translate replaces parts of a string, but unlike replace, translate only works<br />
with single characters. Its strength lies in that it can perform several replacements simultaneously,<br />
and can do so more efficiently than replace.<br />
There are quite a few rather technical uses for this method (such as translating newline<br />
characters or other platform-dependent special characters), but let’s consider a simpler<br />
(although slightly more silly) example. Let’s say you want <strong>to</strong> translate a plain English text in<strong>to</strong><br />
one with a German accent. To do this, you must replace the character “c” with “k,” and “s” with “z.”<br />
Before you can use translate, however, you must make a translation table. This translation<br />
table is a full listing of which characters should be replaced by which. Because this table (which is<br />
actually just a string) has 256 entries, you won’t write it out yourself: You’ll use the function<br />
maketrans from the string module.<br />
The maketrans function takes two arguments: two strings of equal length, indicating that<br />
each character in the first string should be replaced by the character in the same position in the<br />
second string. Got that? In the case of our simple example, the code would look like the following:<br />
>>> from string import maketrans<br />
>>> table = maketrans('cs', 'kz')<br />
WHAT’S IN A TRANSLATION TABLE?<br />
A translation table is a string containing one replacement letter for each of the 256 characters in the ASCII<br />
character set:<br />
>>> table = maketrans('cs', 'kz')<br />
>>> len(table)<br />
256<br />
>>> table[97:123]<br />
'abkdefghijklmnopqrztuvwxyz'<br />
>>> maketrans('', '')[97:123]<br />
'abcdefghijklmnopqrstuvwxyz'<br />
As you can see, I’ve sliced out the part of the table that corresponds <strong>to</strong> the lowercase letters. Take a look<br />
at the alphabet in the table and that in the empty translation (which doesn’t change anything). The empty<br />
translation has a normal alphabet, while in the preceding code, the letter “c” has been replaced by “k,” and<br />
“s” has been replaced by “z.”<br />
Once you have this table, you can use it as an argument <strong>to</strong> the translate method, thereby<br />
translating your string:<br />
>>> 'this is an incredible test'.translate(table)<br />
'thiz iz an inkredible tezt'