GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • C programming
  • C++ programming
  • Java programming
  • Python programming
  • HTML
  • CSS
  • Codenora IDE (opens in a new tab)
  • About us
  • Pricing
  • Contact us
  • Privacy policy
  • Terms of service
  • Refund and Cancellation

This view

  • Original page
  • New address

GeezerVison is showing www.dremendo.com — input() Function and its Use in Python Program | Dremendo

  • Dremendo
  • C
  • C++
  • Java
  • Python
  • HTML
  • CSS
  • More
    • Home
    • C Tutorial
    • C++ Tutorial
    • Java Tutorial
    • Python Tutorial
    • HTML Tutorial
    • CSS Tutorial
?>
profile image
Announcements
No new announcements.
mentora icon
Python Programming Tutorial
  • Python Basic Concepts
    • Introduction
    • Variable
    • Data Types
    • Naming Conventions
    • Typecasting
  • Operators in Python
    • Operators
    • Assignment Operators
    • Arithmetic Operators
    • Relational Operators
    • Logical Operators
    • Identity Operators
    • Membership Operators
    • Operator Precedence
  • Input and Output in Python
    • Environment Setup
    • print() Function
    • input() Function
    • Comments
  • Decision Making in Python
    • Decision Making
    • if Statement
    • if else Statement
    • if elif Statement
  • Mathematical Functions
    • Mathematical Functions
  • Loops in Python
    • Loops
    • for Loop
    • while Loop
  • String in Python
    • String
    • String Methods
  • Built-in Data Structures in Python
    • Lists
    • Tuples
    • Sets
    • Dictionaries
  • Arrays in Python
    • Arrays
  • Searching in Python
    • Linear Search
    • Binary Search
  • Sorting in Python
    • Bubble Sort
    • Selection Sort
    • Insertion Sort
  • Function in Python
    • Function
    • Recursive Function
    • Lambda Function
    • Closures
    • Decorators
    • Generators
  • OOPs in Python
    • Classes and Objects
    • Instance Variables
    • Instance Methods
    • Class Variables
    • Static Methods
    • Inheritance
    • Types of Inheritance
    • MRO
    • Polymorphism
    • Abstract Classes
  • User-Defined Data Structures in Python
    • Stack
    • Queue
    • Circular Queue
    • Double Ended Queue
    • Singly Linked List
    • Doubly Linked List
    • Stack using Linked List
    • Queue using Linked List
  • Modules in Python
    • Modules
    • Time Module
    • Datetime Module
    • OS Module
    • Shutil Module
    • Sys Module
    • JSON Module
    • Random Module
  • Regular Expression in Python
    • Regular Expression
  • Exceptions in Python
    • Exception Handling
  • File Handling in Python
    • Text File Handling
    • Binary File Handling
    • CSV File Handling

bookmark
Table of Contents

    input() Function and its Use in Python Program

    In 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() Function

    The 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....
    video image

    Let's look at some examples to understand this better.

    Example: Simple Input
    name = input("Enter your name: ")
    print("Hello", name)
    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
    name = input()
    print("Hello", name)
    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
    name = input("Enter your name: ")
    age = input("Enter your age: ")
    print("My name is {} and I am {} years old".format(name, age))
    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 Types

    The 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
    age = int(input("Enter your age: "))
    print("Next year, you will be {} years old.".format(age+1))
    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
    height = float(input("Enter your height in meters: "))
    print("Your height is: {:.2f} meters.".format(height))
    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 Knowledge

    Attempt the questions in the exercise to check whether you have understood the lesson clearly.

    Exercise
    Test Your Knowledge
    Previous Next

    We care about your privacy

    Dremendo 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 Preferences

    Choose which types of cookies Dremendo may use.

    Necessary Cookies

    Required for essential website functionality and security.

    Allow
    Analytics Cookies

    Help us understand how visitors use Dremendo.

    Allow
    Marketing Cookies

    Help us measure advertising performance and understand interactions with our advertisements.

    Allow
    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.