Facebook X (Twitter) Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook X (Twitter) Instagram
    TecAdmin
    You are at:Home»Programming»Python»How to Accept User Input in Python

    How to Accept User Input in Python

    By RahulJuly 18, 20222 Mins Read

    In Python, there are a few ways to accept user input. In this blog post, we will explore three different ways to do so. We will start with the simplest way and work our way up to the more complex methods. By the end of this post, you should have a good understanding of how to accept user input in Python. Let’s get started!

    Python 3 provides a built-in function called input() that allows you to take user input. Where Python 2.x uses the raw_input() function is used to accept user input. Python 2.7 also has a function called input(). However, this function takes only one argument: the prompt string.

    Lets see a few examples:

    • Python 3.x – Using input() function

      1
      2
      3
      name = input("Enter your name: ")
       
      print("Hello", name)

      Open a Python shell and run the above statements.

      Take User input in Python
      Python3 read() function
    • Python 2.x – Using raw_input() function

      1
      2
      3
      name = raw_input("Enter your name: ")
       
      print("Hello", name)

      This is similar to the input() function of Python 3.

      Take User input in Python
      Python 2.7 raw_input() function
    • Python 2.x – Using input() function

      1
      2
      3
      name = input("Enter your name: ")
       
      print("Hello",name)

      Run the above scripts in the Python shell and input a string without any quotes. Python will return an error message. Now again run the same script and input a string with quotes. This will work.

      Take User input in Python
      Python 2.7 input() function

    Python
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    Using .env Files in Django

    How to Dockerize Python FastAPI Application

    How to Create and Use Custom Python Module

    Add A Comment

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Difference Between Full Virtualization vs Paravirtualization
    • Virtualization vs. Containerization: A Comparative Analysis
    • Using .env Files in Django
    • Using .env File in FastAPI
    • Setting Up Email Notifications for Django Error Reporting
    Facebook X (Twitter) Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.