Here are our solutions for the day 3 exercises in the 30 Days of Python series. Make sure you try the exercises yourself before checking out the solutions!
1) Using the variable below, print "Hello, world!"
.
greeting = "Hello, world"
For this exercise, we have a choice to make. How are we going to add this exclamation mark to the string we're trying to print?
We have three main options:
- We can use concatenation to add a string containing just
"!"
togreeting
. - We can use the
format
method passing ingreeting
. - We can take a similar approach to option 2, but using an f-string
First, let's look at format
, because I think format
is by far the worst option in this case.
greeting = "Hello, world"
print("{}!".format(greeting))
The reason I don't like this option is that it's very verbose, and it also makes it very unclear what we're actually changing. The exclamation mark is pretty hidden away in there.
format
can be an extremely useful tool, but it's overkill in this situation.
Next, let's look at f-strings, which offer a far better solution here, in my opinion.
greeting = "Hello, world"
print(f"{greeting}!")
There's a lot less going on here, which makes it much easier to read. I think this is a really good option.
I think my favourite version in this case, however, is to concatenate greeting
with "!"
.
greeting = "Hello, world"
print(greeting + "!")
I like this solution because it makes a lot of intuitive sense. We don't really need to know a lot about code to understand what's going on in this case.
2) Ask the user for their name, and then greet the user, using their name as part of the greeting. The name should be in title case, and shouldn't be surrounded by any excess white space.
First things first, we need to get hold of the person's name. This means we need the input
function.
name = input("Please enter your name: ")
Don't forget to add a little bit of space after your prompt so that the user isn't writing directly next to your prompt text. It makes things much easier to read.
The next step is processing the user's name. We need to remove any excess white space and convert it to title case. That means we need the strip
and title
methods, and we can call this directly on the string we get back from input
.
name = input("Please enter your name: ").strip().title()
If you prefer, you can always do this on another line:
name = input("Please enter your name: ")
name = name.strip().title()
Now that we have the user's name, we have to place into some kind of greeting string. I'm going to use an f-string in this case:
name = input("Please enter your name: ").strip().title()
print(f"Hello, {name}!")
3) Concatenate the string "I am "
and the integer 29
to produce a string which reads "I am 29"
.
First, let's store this integer in a variable so that we can manipulate the value.
age = 29
Now that we have a value to refer to, we can do something like this:
age = 29
print("I am " + str(age))
If you don't want to clutter up print
call too much, you can use some additional variables. Both of these are perfectly reasonable, for example:
age = str(age)
print("I am " + age)
age = 29
output_string = "I am " + str(age)
print(output_string)
4) Format and print the information below using string interpolation.
title = "Joker"
director = "Todd Phillips"
release_year = 2019
For this exercise, our target output is this:
Joker (2019), directed by Todd Phillips
Here, I think the best approach is to use an f-string. Since the data is already nicely named, and we don't have to worry about types with f-strings, the solution is relatively straightforward.
title = "Joker"
director = "Todd Phillips"
release_year = 2019
print(f"{title} ({release_year}), directed by {director}")
Remember that everything inside the curly braces is considered code for an f-string, and each value we want to interpolate should be in its own set of curly braces.