Here are our solutions for the day 7 exercises in the 30 Days of Python series. Make sure you try the exercises yourself before checking out the solutions!
1) Ask the user to enter their given name and surname in response to a single prompt. Use split
to extract the names, and then assign each name to a different variable.
First things first, we need to get the information from the user.
names = input("Please enter your full name: ")
Now that we have the names, we need to split them into a given name and a surname. We can do this with the split
method.
Remember that a function call is an expression, and in the case of input
, the value of that expression is a string containing the characters the user typed in response to our prompt. Since the input
call evaluates to a string, we can call the split
method directly on the input
call.
names = input("Please enter your full name: ").split()
Now names
contains a list of strings, which is what gets returned from split
.
Now we need to split names into two variables, which I'm going to call given_name
and surname
.
names = input("Please enter your full name: ").split()
given_name = names[0]
surname = names[1]
In day 9 we're going to learn about a much better way to do this, so stay tuned.
2) Print the list, [1, 2, 3, 4, 5]
, in the format 1 | 2 | 3 | 4 | 5
using the join
method.
There's one tricky part to this solution, which is that the original list items are numbers. If we want to use the join
method, we need a collection of strings instead. Our first step, is therefore going to be converting each number to a string.
There are lots of ways to go about this, but I'm going to go for a simple for
loop approach. Here I'm going to be going through the items in the original list and appending a string version to a new list.
base_numbers = [1, 2, 3, 4, 5]
processed_numbers = []
for number in base_numbers:
processed_numbers.append(str(number))
Now that we have our collection of strings, we can pass this collection to the join
method. We have to call join
on a string, and that string represents the characters we want to place between the strings in the collection. In this case, we want that string to be " | "
.
base_numbers = [1, 2, 3, 4, 5]
processed_numbers = []
for number in base_numbers:
processed_numbers.append(str(number))
print(" | ".join(processed_numbers))
3) Below you'll find a short list of quotes. Each quote is a string, but each string actually contains quote characters at the start and end. Using slicing, extract the text from each string, without these extra quote marks, and print each quote.
quotes = [
"'What a waste my life would be without all the beautiful mistakes I've made.'",
"'A bend in the road is not the end of the road... Unless you fail to make the turn.'",
"'The very essence of romance is uncertainty.'",
"'We are not here to do what has already been done.'"
]
Since we need to do something for each item, let's start by setting up a for loop to just print the quotes as is:
quotes = [
"'What a waste my life would be without all the beautiful mistakes I've made.'",
"'A bend in the road is not the end of the road... Unless you fail to make the turn.'",
"'The very essence of romance is uncertainty.'",
"'We are not here to do what has already been done.'"
]
for quote in quotes:
print(quote)
Each of the quotes has two characters we want to remove: one quotation mark at the start, and one at the end. Our slice, therefore wants to grab everything from the first index, and should get everything up to, but not including, the final item.
We can write that slice like this: [1:-1]
.
Now we just need to slice each quote when we print it:
quotes = [
"'What a waste my life would be without all the beautiful mistakes I've made.'",
"'A bend in the road is not the end of the road... Unless you fail to make the turn.'",
"'The very essence of romance is uncertainty.'",
"'We are not here to do what has already been done.'"
]
for quote in quotes:
print(quote[1:-1])
Another possible solution we can use here is to call strip
, passing in a single quotation mark.
quotes = [
"'What a waste my life would be without all the beautiful mistakes I've made.'",
"'A bend in the road is not the end of the road... Unless you fail to make the turn.'",
"'The very essence of romance is uncertainty.'",
"'We are not here to do what has already been done.'"
]
for quote in quotes:
print(quote.strip("'"))
4) Ask the user to enter a word, and then print out the length of the word.
The first step here is to get the users word and to clean it up a little bit. We want to make sure we're not counting any whitespace, for example.
word = input("Please enter a word: ").strip()
Once we have a word, getting its length is very easy. We just have to pass it to the len
function and print the result:
word = input("Please enter a word: ").strip()
print(len(word))
For the extension task, we need to find a total character count and a word count. The character count is already taken care of with our original code. We just need to change some variable names.
sample_string = input("Please enter a word: ").strip()
character_count = len(sample_string)
Now we need to find a word count. We're going to take a very simple approach here, and we're just going to split sample_string
using the split
method. This will give us a list of strings created by using whitespace as a delimiter character.
If we find the length of this list, we'll get a rough word count.
sample_string = input("Please enter a word: ").strip()
character_count = len(sample_string)
word_count = len(sample_string.split())
Finally, we need to print the results:
sample_string = input("Please enter a word: ").strip()
character_count = len(sample_string)
word_count = len(sample_string.split())
print(f"Character count: {character_count}")
print(f"Word count: {word_count}")