@@ -85,7 +85,7 @@ it's explained
85
85
[ here] ( https://docs.python.org/3/reference/datamodel.html#special-method-lookup )
86
86
if you want to know more about it.
87
87
88
- ## str() and repr()
88
+ ## String representations
89
89
90
90
You have probably noticed that typing something to the interactive ` >>> `
91
91
prompt is not quite the same thing as printing it. For example,
@@ -99,46 +99,32 @@ hello
99
99
>> >
100
100
```
101
101
102
- Typing ` 'hello' ` without a print displayed it with quotes, but printing
103
- it displayed it without the quotes. Both of these can be customized with
104
- magic methods. ` print('hello') ` does the same thing as
105
- ` print(str('hello')) ` , and typing ` 'hello' ` without a print does roughly
106
- the same thing as ` print(repr('hello')) ` .
102
+ If you want to print something the way it's displayed on the ` >>> `
103
+ prompt you can use the ` repr() ` function. Here "repr" is short for
104
+ "representation".
107
105
108
106
``` python
109
- >> > repr (' hello' ) # a string that literally contains 'hello'
110
- " 'hello'"
111
- >> > str (' hello' ) # same as just 'hello'
112
- ' hello'
113
- >> > print (repr (' hello' ))
114
- ' hello'
115
- >> > print (str (' hello' ))
116
- hello
107
+ >> > message = ' hello'
108
+ >> > print (" the message is" , repr (message))
109
+ the message is ' hello'
117
110
>> >
118
111
```
119
112
120
- The ` repr() ` values can also be easily used with string formatting.
121
- Instead of this...
122
-
123
- ``` python
124
- print (" message is " + repr (message))
125
- ```
126
-
127
- ...you can use one of these instead:
113
+ Combining ` repr() ` with [ string
114
+ formatting] ( ../basics/handy-stuff-strings.md#string-formatting ) is also
115
+ easy. ` % ` formatting has a ` %r ` formatter, and ` .format() ` formatting
116
+ has a ` !r ` flag.
128
117
129
118
``` python
130
- print (" message is %r " % (message,))
131
- print (" message is {!r } " .format(message))
119
+ >> > print (" the message is %r " % (message,))
120
+ the message is ' hello'
121
+ >> > print (" the message is {!r } " .format(message))
122
+ the message is ' hello'
123
+ >> >
132
124
```
133
125
134
- ## Customizing str() and repr() values
135
-
136
- As usual, ` repr(thing) ` does the same thing as ` thing.__repr__() ` and
137
- ` str(thing) ` does the same thing as ` thing.__str__() ` . Usually you don't
138
- need to and you shouldn't define ` __str__ ` yourself because ` __str__ `
139
- defaults to ` __repr__ ` .
140
-
141
- Here's an example that hopefully clarifies things:
126
+ The ` __repr__ ` magic method can be used to customize this. For example,
127
+ we can do this:
142
128
143
129
``` python
144
130
>> > class Website :
@@ -148,16 +134,12 @@ Here's an example that hopefully clarifies things:
148
134
>> > w = Website()
149
135
>> > w.__repr__ ()
150
136
' <a Website object>'
151
- >> > repr (w)
152
- ' <a Website object>'
153
- >> > w.__str__ ()
154
- ' <a Website object>'
155
137
>> > str (w)
156
138
' <a Website object>'
157
- >> > w
158
- < a Website object >
159
139
>> > print (w)
160
140
< a Website object >
141
+ >> > w
142
+ < a Website object >
161
143
>> >
162
144
```
163
145
0 commit comments