Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Interact With User Input (Solution)

00:00 To solve this exercise, you’re going to need to use the input() function in Python to collect the user input and then some string methods to convert it.

00:09 And then also there’s a function called len() that can help you to get the length of the strings. I could do this over here in the REPL now, and I’m probably going to experiment a bit, but I also opened up a file where I’m going to write the code so that I can run it multiple times easier without having to type it out every time.

00:28 And that gives me a chance to actually also test and see whether my script works, not just for one input, but for repeated different inputs. So I’m going to write the code in the script, but you can also write it over in the REPL if you want.

00:41 That’s just as fine. And here in the script, I’m going to take the notes. I want to collect user input, then I want to convert it to lowercase

00:56 and also print the length of the input and convert it to lowercase and display it.

01:06 So these are the three tasks that my script should do. I’m going to start off with number one, where I’ll use input() with a little prompt in here. The prompt is going to say "Type something: "

01:20 and I’m adding this space here to format it a bit nicer.

01:24 And then I need to collect the result of this. So if I just run input() like that, let’s try it out over in the REPL. I’ll get the prompt that says Type something: and I can say hello, and it gets returned.

01:39 But in my script, I’m currently not collecting it anywhere. Or also here in the REPL, I wasn’t collecting it anywhere. So I need to assign it to a variable so that I can then do something with it, like lowercase it and display it and get its length, et cetera.

01:54 So back in my script on line 2, I’m going to say user_input = the return of calling input() and whatever then the user types in there.

02:07 And then I want to convert it to lowercase and display it. So let’s start with this. I will use the print() function by typing print, opening the parentheses, and then putting in user_input.lower().

02:21 So this is just the .lower() string method that you’ve used a couple of times by now and displaying it back to the user by using the print() function. I’m going to test this script by running it in IDLE.

02:35 You can press F5 to run it. Save it and run it. So we’ll do that. And we can see over here in the REPL, I get the prompt to type something. So I’m going to try again to say HELLLLLOOOOO in all uppercase.

02:49 And you can see that my script works. I get back the same string, but in all lowercase. Another advantage of having a script here is I can just run this again without needing to type anything else.

03:00 I get another prompt, and I can try out mixed case, hEllOOOO. And again, I just get it back in lowercase. And again, I just get it back in lowercase.

03:10 So this is working. Now I’m going to solve the task on line 5, print the length of the input as well.

03:17 For this, I can just use the len() function. The len() function works that it takes in a collection—for example, a string—and then it just returns the number of items in that collection.

03:29 So if I put in a string here and say "hello", I get back 5, which is the number of characters: 1, 2, 3, 4, 5. So that’s exactly what I want.

03:41 And back in my script on line 6, I’m going to say print, open up the parentheses and then I will say len(), because that’s what I want to print out. To len() as an argument, I’m going to pass user_input. Note that the user_input that I’m passing here is actually the original one, so it’s not the lowercase one, but it doesn’t matter because .lower() is not going to change the number of characters.

04:04 It’s really just going to lowercase it. So both the lowercase version and whatever the user-input-cased version are going to have the same amount of characters.

04:16 And now I’m going to test the script again by running it. Save and run. Type something. I already know that hello has 5, so we will use that and type a mixed-case HeLLo.

04:27 Press Enter. And as an output, I get the lowercase version of that string and the number of characters in that string.

04:33 Great. That solves all three tasks in here. I’m going to go ahead and clean up the script,

04:43 and that’s my little analyze script.

Become a Member to join the conversation.