Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook 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!

    Advertisement

    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

    How to Create and Read List in Python

    What are the Python Static Variables

    Python Program to Add Two Numbers

    Add A Comment

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • How to List Manually Installed Packages in Ubuntu & Debian
    • 10 Bash Tricks Every Developer Should Know
    • How to Validate Email Address in JavaScript
    • Firewalld: Common Firewall Rules and Commands
    • 12 Apk Commands in Alpine Linux Package Management
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

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