Beginning Python - From Novice to Professional
Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional
CHAPTER 3 ■ WORKING WITH STRINGS 59 price_width = 10 item_width = width - price_width header_format = '%-*s%*s' format = '%-*s%*.2f' print '=' * width print header_format % (item_width, 'Item', price_width, 'Price') print '-' * width print format % (item_width, 'Apples', price_width, 0.4) print format % (item_width, 'Pears', price_width, 0.5) print format % (item_width, 'Cantaloupes', price_width, 1.92) print format % (item_width, 'Dried Apricots (16 oz.)', price_width, 8) print format % (item_width, 'Prunes (4 lbs.)', price_width, 12) print '=' * width The following is a sample run of the program: Please enter width: 35 =================================== Item Price —————————————————————————————— Apples 0.40 Pears 0.50 Cantaloupes 1.92 Dried Apricots (16 oz.) 8.00 Prunes (4 lbs.) 12.00 =================================== String Methods You have already encountered methods in lists. Strings have a much richer set of methods, in part because strings have “inherited” many of their methods from the string module where they resided as functions in earlier versions of Python (and where you may still find them, if you feel the need).
60 CHAPTER 3 ■ WORKING WITH STRINGS BUT STRING ISN’T DEAD Even though string methods have completely upstaged the string module, the module still includes a few constants and functions that aren’t available as string methods. The maketrans function is one example and will be discussed together with the translate method in the material that follows. Table 3-2 shows some useful constants available from string. For a more thorough description of the module, check out Section 4.1 of the Python Library Reference (http://python.org/doc/lib/module-string.html). Table 3-2. Useful Values from the string Module Constant Description string.digits A string containing the digits 0–9 string.letters A string containing all letters (upper- and lowercase) string.lowercase A string containing all lowercase letters string.printable A string containing all printable characters string.punctuation A string containing all punctuation characters string.uppercase A string containing all uppercase letters Because there are so many string methods, only some of the most useful ones are described here. For a full reference, see Appendix B. In the description of the string methods, you will find references to other, related string methods in this chapter (marked “See also”) or in Appendix B. find The find method finds a substring within a larger string. It returns the leftmost index where the substring is found. If it is not found, –1 is returned: >>> 'With a moo-moo here, and a moo-moo there'.find('moo') 7 >>> title = "Monty Python's Flying Circus" >>> title.find('Monty') 0 >>> title.find('Python') 6 >>> title.find('Flying') 15 >>> title.find('Zirquss') -1 In our first encounter with membership in Chapter 2, we created part of a spam filter by using the expression '$$$' in subject. We could also have used find (which would also have
- Page 40 and 41: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 42 and 43: CHAPTER 1 ■ INSTANT HACKING: THE
- 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 92 and 93: CHAPTER 3 ■ WORKING WITH STRINGS
- Page 94 and 95: 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
CHAPTER 3 ■ WORKING WITH STRINGS 59<br />
price_width = 10<br />
item_width = width - price_width<br />
header_format = '%-*s%*s'<br />
format = '%-*s%*.2f'<br />
print '=' * width<br />
print header_format % (item_width, 'Item', price_width, 'Price')<br />
print '-' * width<br />
print format % (item_width, 'Apples', price_width, 0.4)<br />
print format % (item_width, 'Pears', price_width, 0.5)<br />
print format % (item_width, 'Cantaloupes', price_width, 1.92)<br />
print format % (item_width, 'Dried Apricots (16 oz.)', price_width, 8)<br />
print format % (item_width, 'Prunes (4 lbs.)', price_width, 12)<br />
print '=' * width<br />
The following is a sample run of the program:<br />
Please enter width: 35<br />
===================================<br />
Item<br />
Price<br />
——————————————————————————————<br />
Apples 0.40<br />
Pears 0.50<br />
Cantaloupes 1.92<br />
Dried Apricots (16 oz.) 8.00<br />
Prunes (4 lbs.) 12.00<br />
===================================<br />
String Methods<br />
You have already encountered methods in lists. Strings have a much richer set of methods, in<br />
part because strings have “inherited” many of their methods from the string module where<br />
they resided as functions in earlier versions of <strong>Python</strong> (and where you may still find them, if<br />
you feel the need).