Beginning Python - From Novice to Professional
Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional
CHAPTER 1 ■ INSTANT HACKING: THE BASICS 25 String Representations, str and repr Throughout these examples, you have probably noticed that all the strings printed out by Python are still quoted. That’s because it prints out the value as it might be written in Python code, not how you would like it to look for the user. If you use print, however, the result is different: >>> "Hello, world!" 'Hello, world!' >>> 10000L 10000L >>> print "Hello, world!" Hello, world! >>> print 10000L 10000 As you can see, the long integer 10000L is simply the number 10000 and should be written that way when presented to the user. But when you want to know what value a variable refers to, you may be interested in whether it’s a normal integer or a long, for example. What is actually going on here is that values are converted to strings through two different mechanisms. You can use both mechanisms yourself, through the functions str, which simply converts a value into a string in some reasonable fashion that will probably be understood by a user, for example, and repr, which creates a string that is a representation of the value as a legal Python expression: 9 >>> print repr("Hello, world!") 'Hello, world!' >>> print repr(10000L) 10000L >>> print str("Hello, world!") Hello, world! >>> print str(10000L) 10000 A synonym for repr(x) is `x` (here you use backticks, not single quotes). This can be useful when you want to print out a sentence containing a number, for example: >>> temp = 42 >>> print "The temperature is " + temp Traceback (most recent call last): File "", line 1, in ? print "The temperature is " + temp TypeError: cannot add type "int" to string >>> print "The temperature is " + `temp` The temperature is 42 The first print statement doesn’t work because you can’t add a string to a number. The second one, however, works because I have converted temp to the string "42" by using the backticks. 9. Actually, str is a type, just like int and long. repr, however, is simply a function.
26 CHAPTER 1 ■ INSTANT HACKING: THE BASICS (I might, of course, just as well have used repr, which means the same thing, but may be a bit clearer. Actually, in this case, I could also have used str. Don’t worry too much about this right now.) In short: str, repr, and backticks are three ways of converting a Python value to a string. The function str makes it look good, while repr (and the backticks) tries to make the resulting string a legal Python expression. input vs. raw_input Now you know what "Hello, " + name + "!" means. But what about raw_input? Isn’t input good enough? Let’s try it. Enter the following in a separate script file: name = input("What is your name? ") print "Hello, " + name + "!" This is a perfectly valid program, but as you will soon see, it’s a bit unpractical. Let’s try to run it: What is your name? Gumby Traceback (most recent call last): File "C:/python/test.py", line 2, in ? name = input("What is your name? ") File "", line 0, in ? NameError: name 'Gumby' is not defined The problem is that input assumes that what you enter is a valid Python expression (it’s more or less the inverse of repr). If you write your name as a string, that’s no problem: What is your name? "Gumby" Hello, Gumby! However, it’s just a bit too much to ask that the user write his or her name in quotes like this; therefore we use raw_input, which treats all input as raw data, and puts it into a string: >>> input("Enter a number: ") Enter a number: 3 3 >>> raw_input("Enter a number: ") Enter a number: 3 '3' Unless you have a special need for input, you should probably use raw_input. Long Strings, Raw Strings, and Unicode Before ending this chapter, I want to first tell you about yet another couple of ways of writing strings. These alternate string syntaxes can be useful when you have strings that span several lines, or that contain various special characters.
- Page 7 and 8: ■CHAPTER 23 Project 4: In the New
- Page 9 and 10: viii ■CONTENTS Strings . . . . .
- Page 11 and 12: x ■CONTENTS ■CHAPTER 5 Conditio
- Page 13 and 14: xii ■CONTENTS ■CHAPTER 8 Except
- Page 15 and 16: xiv ■CONTENTS ■CHAPTER 11 Files
- Page 17 and 18: xvi ■CONTENTS Dynamic Web Pages w
- Page 19 and 20: xviii ■CONTENTS ■CHAPTER 20 Pro
- Page 21 and 22: xx ■CONTENTS Second Implementatio
- Page 23 and 24: xxii ■CONTENTS Preparations . . .
- Page 26: About the Technical Reviewer ■JER
- Page 30 and 31: Introduction A C program is like a
- Page 32 and 33: CHAPTER 1 ■ ■ ■ Instant Hacki
- 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 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 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
26 CHAPTER 1 ■ INSTANT HACKING: THE BASICS<br />
(I might, of course, just as well have used repr, which means the same thing, but may be a bit<br />
clearer. Actually, in this case, I could also have used str. Don’t worry <strong>to</strong>o much about this<br />
right now.)<br />
In short: str, repr, and backticks are three ways of converting a <strong>Python</strong> value <strong>to</strong> a string.<br />
The function str makes it look good, while repr (and the backticks) tries <strong>to</strong> make the resulting<br />
string a legal <strong>Python</strong> expression.<br />
input vs. raw_input<br />
Now you know what "Hello, " + name + "!" means. But what about raw_input? Isn’t input<br />
good enough? Let’s try it. Enter the following in a separate script file:<br />
name = input("What is your name? ")<br />
print "Hello, " + name + "!"<br />
This is a perfectly valid program, but as you will soon see, it’s a bit unpractical. Let’s try <strong>to</strong><br />
run it:<br />
What is your name? Gumby<br />
Traceback (most recent call last):<br />
File "C:/python/test.py", line 2, in ?<br />
name = input("What is your name? ")<br />
File "", line 0, in ?<br />
NameError: name 'Gumby' is not defined<br />
The problem is that input assumes that what you enter is a valid <strong>Python</strong> expression (it’s more<br />
or less the inverse of repr). If you write your name as a string, that’s no problem:<br />
What is your name? "Gumby"<br />
Hello, Gumby!<br />
However, it’s just a bit <strong>to</strong>o much <strong>to</strong> ask that the user write his or her name in quotes like this;<br />
therefore we use raw_input, which treats all input as raw data, and puts it in<strong>to</strong> a string:<br />
>>> input("Enter a number: ")<br />
Enter a number: 3<br />
3<br />
>>> raw_input("Enter a number: ")<br />
Enter a number: 3<br />
'3'<br />
Unless you have a special need for input, you should probably use raw_input.<br />
Long Strings, Raw Strings, and Unicode<br />
Before ending this chapter, I want <strong>to</strong> first tell you about yet another couple of ways of writing<br />
strings. These alternate string syntaxes can be useful when you have strings that span several<br />
lines, or that contain various special characters.