@@ -17,11 +17,11 @@ Here is an example of formatting the string with values from the person. This is
17
17
18
18
.. code :: python
19
19
20
- person = {' first' :' Tobin' , ' age' :20 }
21
- print (' {0} is {1} years old' .format(person[' first' ], person[' age' ])) # bad
20
+ person = {' first' :' Tobin' , ' age' :20 }
21
+ print (' {0} is {1} years old' .format(person[' first' ], person[' age' ])) # bad
22
22
23
- person = {' first' :' Tobin' , ' last' : ' Brown' , ' age' :20 }
24
- print (' {0} {1} is {2} years old' .format(person[' first' ], person[' last' ], person[' age' ])) # bad
23
+ person = {' first' :' Tobin' , ' last' : ' Brown' , ' age' :20 }
24
+ print (' {0} {1} is {2} years old' .format(person[' first' ], person[' last' ], person[' age' ])) # bad
25
25
26
26
27
27
Best practice
@@ -31,7 +31,7 @@ By using the dictionary keys in the string we are formatting, the code is much m
31
31
32
32
.. code :: python
33
33
34
- person = {' first' :' Tobin' , ' age' :20 }
34
+ person = {' first' :' Tobin' , ' age' :20 }
35
35
print (' {first} is {age} years old' .format(** person))
36
36
37
37
person = {' first' :' Tobin' , ' last' : ' Brown' , ' age' :20 }
@@ -42,16 +42,16 @@ Going even further, the same result can be achieved with your own objects by usi
42
42
43
43
.. code :: python
44
44
45
- class Person (object ):
45
+ class Person (object ):
46
46
47
- def __init__ (self , first , last , age ):
48
- self .first = first
49
- self .last = last
50
- self .age = age
47
+ def __init__ (self , first , last , age ):
48
+ self .first = first
49
+ self .last = last
50
+ self .age = age
51
51
52
- def __str__ (self ):
53
- return ' {first} {last} is {age} years old' .format(** self .__dict__ )
52
+ def __str__ (self ):
53
+ return ' {first} {last} is {age} years old' .format(** self .__dict__ )
54
54
55
55
56
- person = Person(' Tobin' , ' Brown' , 20 )
57
- print (person)
56
+ person = Person(' Tobin' , ' Brown' , 20 )
57
+ print (person)
0 commit comments