Beginning Python - From Novice to Professional
Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional
CHAPTER 3 ■ ■ ■ Working with Strings You’ve seen strings before, and know how to make them. You’ve also looked at how to access their individual characters by indexing and slicing. In this chapter, you see how to use them to format other values (for printing, for example), and take a quick look at the useful things you can do with string methods, such as splitting, joining, searching, and more. Basic String Operations All the standard sequence operations (indexing, slicing, multiplication, membership, length, minimum, and maximum) work with strings, as you saw in the previous chapter. Remember, however, that strings are immutable, so all kinds of item or slice assignments are illegal: >>> website = 'http://www.python.org' >>> website[-3:] = 'com' Traceback (most recent call last): File "", line 1, in ? website[-3:] = 'com' TypeError: object doesn't support slice assignment String Formatting: The Short Version If you are new to Python programming, chances are you won’t need all the options that are available in Python string formatting, so I’ll give you the short version here. If you are interested in the details, take a look at the section “String Formatting: The Long Version,” which follows. Otherwise, just read this and skip down to the section “String Methods.” String formatting is done with the string formatting operator, the percent (%) sign. ■Note As you may remember, % is also used as a modulus (remainder) operator. To the left of it you place a string (the format string), and to the right of it you place the value you want to format. You can either use a single value such as a string or a number, or you 53
54 CHAPTER 3 ■ WORKING WITH STRINGS can use a tuple of values (if you want to format more than one), or, as I discuss in the next chapter, you can use a dictionary. The most common case is the tuple: >>> format = "Hello, %s. %s enough for ya?" >>> values = ('world', 'Hot') >>> print format % values Hello, world. Hot enough for ya? ■Note If you use a list or some other sequence instead of a tuple, the sequence will be interpreted as a single value. Only tuples and dictionaries (discussed in Chapter 4) will allow you to format more than one value. The %s parts of the format string are called conversion specifiers. They mark the places where the values are to be inserted. The s means that the values should be formatted as if they were strings—if they aren’t, they’ll be converted with str. This works with most values; for a list of other specifier types, see Table 3-1 later in the chapter. ■Note To actually include a percent sign in the format string, you must write %% so Python doesn’t mistake it for the beginning of a conversion specifier. If you are formatting real numbers (floats), you can use the f specifier type and supply the precision as a . (dot) followed by the number of decimals you want to keep. The format specifier always ends with a type character, so you must put the precision before that: >>> format = "Pi with three decimals: %.3f" >>> from math import pi >>> print format % pi Pi with three decimals: 3.142 TEMPLATE STRINGS The string module offers another way of formatting values: template strings. They work more like variable substitution in many UNIX shells, with $foo being replaced by a keyword argument called foo that is passed to the template method substitute: 1 >>> from string import Template >>> s = Template('$x, glorious $x!') >>> s.substitute(x='slurm') 'slurm, glorious slurm!' 1. For more about keyword arguments, see Chapter 6.
- Page 34 and 35: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 36 and 37: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 38 and 39: CHAPTER 1 ■ INSTANT HACKING: THE
- 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 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 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
CHAPTER 3<br />
■ ■ ■<br />
Working with Strings<br />
You’ve seen strings before, and know how <strong>to</strong> make them. You’ve also looked at how <strong>to</strong> access<br />
their individual characters by indexing and slicing. In this chapter, you see how <strong>to</strong> use them <strong>to</strong><br />
format other values (for printing, for example), and take a quick look at the useful things you<br />
can do with string methods, such as splitting, joining, searching, and more.<br />
Basic String Operations<br />
All the standard sequence operations (indexing, slicing, multiplication, membership, length,<br />
minimum, and maximum) work with strings, as you saw in the previous chapter. Remember,<br />
however, that strings are immutable, so all kinds of item or slice assignments are illegal:<br />
>>> website = 'http://www.python.org'<br />
>>> website[-3:] = 'com'<br />
Traceback (most recent call last):<br />
File "", line 1, in ?<br />
website[-3:] = 'com'<br />
TypeError: object doesn't support slice assignment<br />
String Formatting: The Short Version<br />
If you are new <strong>to</strong> <strong>Python</strong> programming, chances are you won’t need all the options that are<br />
available in <strong>Python</strong> string formatting, so I’ll give you the short version here. If you are interested in<br />
the details, take a look at the section “String Formatting: The Long Version,” which follows.<br />
Otherwise, just read this and skip down <strong>to</strong> the section “String Methods.”<br />
String formatting is done with the string formatting opera<strong>to</strong>r, the percent (%) sign.<br />
■Note As you may remember, % is also used as a modulus (remainder) opera<strong>to</strong>r.<br />
To the left of it you place a string (the format string), and <strong>to</strong> the right of it you place the<br />
value you want <strong>to</strong> format. You can either use a single value such as a string or a number, or you<br />
53