Python Strings Practice Opportunity Solution
Python Strings Practice Opportunity Solution
[00:00:07.74] RYAN AHMED: Hello, everyone. And welcome to this Practice Opportunity
Solution lecture. I hope you have been able to solve the practice opportunity. Let me go ahead
and walk you through the question and the solution. So assume that you work at the bank and
you have been tasked to develop a python code that asks clients to provide their feedback
regarding the bank service. Simply, the code is going to take the input from the user and it's
going to break their input into individual words.
[00:00:38.80] So here are the tasks that I'm asking you to do. First, I asked you to prompt the
client to enter his or her feedback regarding the service and then obtain their input. Afterwards, I
asked you to split the client input into words and print them out to the screen. And then, I asked
you to test your code using three different user inputs. And then finally, I want you to expand on
your code to request the user to enter his or her email address.
[00:01:09.03] And then, the code should prompt clients requesting his or her email. And then it's
going to extract only the first name from their email. So maybe the point four might be a little bit
tricky. So let's go ahead and show you the solution. First, I wanted to get data from the user. And
to be able to get data from the user, you can use the input function.
[00:01:33.38] So here, I'm going to say feedback. That is going to be the variable where I'm
going to place what the input-- what the user is going to input. And then I'm going to say input,
open parentheses, and that opened quotations. And then I'm going to provide a message to the
user. So here, I'm going to say, welcome to the bank. What did we do at the bank today--
something along those lines. What did we do at the bank today?
[00:02:04.44] And you can go and change the E here to-- welcome, for example-- to e lowercase.
OK? So that is going to simply prompt the user to enter their feedback. And then, I'm going to
receive that feedback and put it in a feedback. So if you actually do that-- if you press Shift Enter
right now-- you will see that right now, we are stuck in a loop as shown here on the left hand
side.
[00:02:28.63] You will that there's an asterisk, meaning that simply the code right now is waiting
for an input from the user. So here, I'm going to say, well, I am super happy with the service--
something along those lines. You must press Enter to be able to execute or run the cell. And you
should be able to see a number here indicating that this cell ran successfully. And you will see
that simply in feedback, now I got I am super happy with the service. And this is simply the text
that I inserted using my keyboard. OK.
[00:03:00.43] So what I wanted to do next is I wanted to split the input that the user is going to
get into words. We learned how to do that in the past. Simply, I'm going to say words equals to.
I'm going to grab the feedback. And this is simply the input that the user has entered. And if I say
dot split and you open parentheses, you can end-- for example-- space here. I'm going to say,
OK. Please go ahead split my data based on the space. And then, I'm going to print here the
words.
[00:03:31.69] So I'm going to say print, Here are the lists of words you entered-- something
along those lines. And then, I'm going to say, please go ahead and print out the words for me. So
this is simply is going to be my list that I'm going to split. So if you actually run it again-- if you
press Shift Enter-- and I say, well, I am super happy with the service, OK, and then you press
Enter-- here we go.
[00:04:02.42] And now, if I run this one right now-- here we go. Now you notice that simply I
got the string from the user. I split it into words. And now, I have I on its own, and on its own,
super happy with the surface, OK? So that's exactly what I'm looking for. OK. The next part of
the question, I asked you to test your code using three different user inputs.
[00:04:24.69] So here, I tried with input. Maybe I'm going to try another one for you. So I'm
going to say, welcome to the bank. Say, for example, I am-- let's say I'm not happy with the
service-- something along this line. And you press Enter. Then you can check it out again. Shift
Enter-- here we go. Now, we ended up with I have not happy with the service. OK. Next, I asked
you to extend your code to request the user to enter his or her email. And the email format is
going to be first name dot last name at gmail.com.
[00:04:58.56] And then, I'm going to split my email simply into first name, last name, and then I
have the gmail.com. To do that-- basically, the way to think about this problem is I need first to
separate the first name dot the last name of the email from the actual gmail.com afterwards. So to
do that, I'm going to use the at here to split my email first in half.
[00:05:25.06] So I'm going to end up with a list containing the first part of the email here before
the at sign, and then the gmail.com-- which is after at sign. Once I do that, I'm going to grab the
first element in my list here. And then I'm going to split it further based on the dot here-- or the
period. And that is going to obtain the first name here for me. So let me show you how we can
achieve that. So what I'm going to do is I'm going to say email equals to. And then I'm going to
say input, open parentheses.
[00:05:58.59] And then I'm going to ask the user to please enter your email. So the user is going
to enter their email address. Let's call it that. And let's go ahead and set that to lowercase. And
then, I'm going to get the email, right? So once I give the email from the user, I'm going to
simply split it into what's before the at and what's after the at. To do that, I'm going to say output
equals to, program my email. And then I'm going to say dot split, open parentheses, add
quotations. And then, I'm going to specify the at here.
[00:06:35.38] So if you press Shift and Enter, and now you can insert your email. For example,
I'm going to say Ryan, for example. You can put your name in there. And then, you can say at
gmail dot. You press Enter. Now I got the output. If you want to see what's in output right now,
if you press Shift Enter, here we go. Basically, what you get is that you get the first part of the
email-- which is ryan.ahmed here. And then the last piece is going to be the gmail.com.
[00:06:58.72] What I'm interested in is I'm interested to grab the first element here in my list.
And simply, I wanted to split it further based on the period based on that dot. If you press A, that
is going to insert a couple of cells for you. So what I'm going to do to access specific element in
a python list-- if you recall, we have done that before-- you access them with their index. For
example, if I would like to access this element here, well, the first element in a python list has an
index of 0.
[00:07:30.58] So if I grab my output, and I open square bracket, and then I say 0, you press Shift
Enter-- simply, you will get this element here, which is the one I'm interested in. So if I grab that
and if I say dot split based on the period, OK, and I'm going to put that in name-- for example
equals 2. And then, if you grab the name-- if you press Shift Enter-- here we go. Now we have
been able to split the first element in my original list here output into Ryan and then Ahmed, for
example.
[00:08:03.39] And then now, you can go ahead and then you can simply grab the first element in
name. Press Shift Enter, here we go. Now we have been able to obtain only the first element
from the email that the user has entered. And of course, you can go ahead and check it out again.
For example, maybe the name might be, let's say, Max dot, let's say, Cheng for example--
something along those lines-- at gmail.com.
[00:08:29.46] You press Enter. And if you want to check it out, here we go. Now we split it
based on the at. And then you do further splitting. And you end up with Max only, which is my
first name here in the email. And that's it. That's all I have for this lesson. I hope you enjoyed it
and see you in future lessons.