Throughout this lecture, you're going to learn how to insert some input into a Python program. Actually, you're going to use a specific function in order to ask the user for input to store the information he's entering at the prompt, and then use that information further into the program. This is especially useful when you need to build an interactive application, usually having some sort of menu that the user needs to interact with. Examples of such menus are Please enter your username, or choose an option from the following list kind of menu. Okay, let's get to work. The function I'm talking about is called input.
But for starters, let's create a Python file on my D drive called file to.py. And prompt the user to enter a string that he wants to be printed out on the screen. So file two and now let's change The extension from txt to BI. We are asked if we want to indeed change this extension. And we click Yes. Now let's open up the file edit with notepad plus plus.
Now I'm going to write some code using the input function I told you about. Let me write this line and then we will analyze it inch by inch. Looking at this line of code, you may ask yourself, okay, but what is this user says thing right here? Well, that's a Python variable. And don't worry, we will talk more about variables very, very soon. For now, just keep in mind that by using a variable, you can quote store or save, unquote, the value entered by the user for later use.
This so called storing or saving of the user's input is accomplished using the equal sign right here, which is called an assignment operator. But more on that later in this section. Following the equal sign, we have the input function and Again, don't worry, we will also discuss functions extensively in this course. For now let's just focus on the use case of this code. Next inside inputs pair of parentheses, you have to type in a description, a phrase, which is actually a string, asking the user for input. This is completely up to you to come up with an appropriate sentence.
A good practice here is to also enter a colon and a space after the text. So when the user input some data, it will be clearly separated from the sentence you just wrote, just to make everything pretty and easy to read. Finally, do not forget to enclose everything in between parentheses, also using either double quotes right here, or single quotes. Last but not least, in order to have our text printed out on the screen, and visible to the user, we should use the print function in Python. To print out the content of the user says variable To do that, I'm just going to write, print user says, OK, that's it. Now let's save the file using Ctrl S, and run it inside the windows command line cmd.
To run the file, we just have to do what we previously did when running file, one dot p y. So by Thawne, the name of the file and also the py extension, Enter. And you can now see that the program nicely asks you to enter whatever you want to be displayed on the screen. So let's try a couple of strings. Let's say hello, Python. Let's try another string.
Python is the best suite It works like a charm. So keep in mind that you are able to capture user input, display this input on the screen or even store it using a variable and later reuse it across your script. All this with the help of the input function. See you in the next lecture.