@@ -89,6 +89,69 @@ isn't exactly like mine but it works just fine it's ok, and you can
89
89
This is not a good way to ask a password from the user because the
90
90
password isn' t hidden in any way, but this is just an example.
91
91
92
+ # # Handy stuff: Strings
93
+
94
+ 1 . The program is not like you might expect it to be. It actually works
95
+ just fine if we run it, but there' s the problem. The last line is
96
+ really long and it' s hard to see what it does.
97
+
98
+ The solution is string formatting. At the time of writing this, I
99
+ recommend replacing the last line with one of these:
100
+
101
+ ```py
102
+ print (" You entered %s , %s , %s and %s ." % (word1, word2, word3, word4))
103
+ print (" You entered {} , {} , {} and {} ." .format(word1, word2, word3, word4))
104
+ ```
105
+
106
+ In the future when most people will have Python 3.6 or newer, you
107
+ can also use this:
108
+
109
+ ```py
110
+ print (f " You entered { word1} , { word2} , { word3} and { word4} . " )
111
+ ```
112
+
113
+ 2 . If we have a look at `help (str .upper)` it looks like this:
114
+
115
+ S.upper() -> str
116
+
117
+ Return a copy of S converted to uppercase.
118
+
119
+ We have two problems. First of all , the broken code uses
120
+ `message.upper` instead of `message.upper()` . It also expects the
121
+ message to magically make itself uppercased, but strings can' t be
122
+ changed in - place so it doesn' t work.
123
+
124
+ The solution is to do `message.upper()` and save the value we got
125
+ from that to a variable:
126
+
127
+ ```py
128
+ message = input (" What do you want me to say? " )
129
+ uppermessage = message.upper()
130
+ print (uppermessage, " !!!" )
131
+ print (uppermessage, " !!!" )
132
+ print (uppermessage, " !!!" )
133
+ ```
134
+
135
+ Or we can reuse the same variable name:
136
+
137
+ ```py
138
+ message = input (" What do you want me to say? " )
139
+ message = message.upper()
140
+ print (message, " !!!" )
141
+ print (message, " !!!" )
142
+ print (message, " !!!" )
143
+ ```
144
+
145
+ Or we can convert the message to uppercase right away on the first
146
+ line:
147
+
148
+ ```py
149
+ message = input (" What do you want me to say? " ).upper()
150
+ print (message, " !!!" )
151
+ print (message, " !!!" )
152
+ print (message, " !!!" )
153
+ ```
154
+
92
155
# # Loops
93
156
94
157
1 . For loop over the users, each user is a list that contains a
@@ -262,6 +325,7 @@ isn't exactly like mine but it works just fine it's ok, and you can
262
325
return name
263
326
264
327
print (" Your name is" , ask_name())
328
+ ```
265
329
266
330
2 . If you run the next example, you get something like this:
267
331
0 commit comments