Skip to content

Commit 7628d74

Browse files
committed
way better string exercises
1 parent 2a9cd88 commit 7628d74

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

answers.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,69 @@ isn't exactly like mine but it works just fine it's ok, and you can
8989
This is not a good way to ask a password from the user because the
9090
password isn't hidden in any way, but this is just an example.
9191

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+
92155
## Loops
93156

94157
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
262325
return name
263326

264327
print("Your name is", ask_name())
328+
```
265329

266330
2. If you run the next example, you get something like this:
267331

handy-stuff-strings.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,29 @@ ValueError: could not convert string to float: 'hello'
401401
- We can use `str`, `int` and `float` to convert values to different
402402
types.
403403

404+
## Exercises
405+
406+
1. Fix this program.
407+
408+
```py
409+
print("Hello!")
410+
word1 = input("Enter something: ")
411+
word2 = input("Enter another thing: ")
412+
word3 = input("Enter a third thing: ")
413+
word4 = input("And yet another thing: ")
414+
print("You entered " + word1 + ", " + word2 + ", " + word3 + " and " + word4 + ".")
415+
```
416+
417+
2. This program is supposed to say something loudly. Fix it.
418+
419+
```py
420+
message = input("What do you want me to say? ")
421+
message.upper
422+
print(message, "!!!")
423+
print(message, "!!!")
424+
print(message, "!!!")
425+
```
426+
404427
***
405428

406429
You may use this tutorial freely at your own risk. See

0 commit comments

Comments
 (0)