GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.dremendo.com — input() Function and its Use in Python Program | Dremendo
?>
Announcements
No new announcements.
Python Programming Tutorial
Table of Contents
input() Function and its Use in Python ProgramIn this lesson, we will understand what input() function is and how it works in Python programming with the help of some examples. What is input() FunctionThe input() function in Python is used to accept input from the standard input device, typically the keyboard, and store it in a variable. Unlike the print() function, which displays data on the screen, the input() function reads data entered by the user from the keyboard.
Note
The input() function always returns the user’s input as a string, so you may need to convert the input to other data types if necessary.
Video Explanation
Click the button below to watch the explanation.
Please wait while the video is loading....
Let's look at some examples to understand this better.
Example: Simple Input
Output
Enter your name: Peter Hello Peter In this example, we used the input() function to take input from the user. Inside the parentheses of the input() function, we wrote the text Enter your name, which will be displayed on the screen when you run the program. After taking input from the user, we stored it in the variable name, and then we printed the name on the screen using the print() function. Including a message inside the input() function is optional. If you don't want to display a message, simply leave the parentheses empty.
Example: Simple Input without Message
Output
Thomas Hello Thomas We can store the user’s input in multiple variables and use it in different parts of our program.
Example: Storing Multiple Inputs in Variables
Output
Enter your name: Alice Enter your age: 15 My name is Alice and I am 15 years old Converting User Input to Other Data TypesThe input() function in Python always returns the user’s input as a string. Therefore, if we want to use the input as an integer or a floating-point number, we must explicitly convert it to the desired type using functions like int(), float(), and so on.
Example: Converting Input to an Integer
Output
Enter your age: 15 Next year, you will be 16 years old. In this case, the user’s input is converted from a string to an integer using the int() function, allowing arithmetic operations.
Example: Converting Input to a Float
Output
Enter your height in meters: 1.72315 Your height is: 1.72 meters. In this case, the user’s input is converted from a string to a float using the float() function, allowing for handling decimal values. Test Your KnowledgeAttempt the questions in the exercise to check whether you have understood the lesson clearly.
We care about your privacyDremendo uses cookies and similar technologies that are necessary for the site to work properly, understand how you interact with our website, and measure the effectiveness of our marketing. By clicking Accept All, you consent to the use of non-essential cookies and similar technologies for analytics, advertising, and ad measurement, as described in our Privacy Policy. Cookie PreferencesChoose which types of cookies Dremendo may use.
Necessary Cookies
Allow
Required for essential website functionality and security.
Analytics Cookies
Allow
Help us understand how visitors use Dremendo.
Marketing Cookies
Allow
Help us measure advertising performance and understand interactions with our advertisements.
Mentora
Question Asked: 0 / 20
Hi, I’m Mentora! I’m your personal AI tutor from Dremendo. Ask me anything, and I’ll help you understand it in a simple and easy way. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |