how to enter input in python

18,808

Solution 1

There's more than one way to do this.

Taking input from the intepreter

For Python 2.x, you can use the raw_input() function:

my_input = raw_input("Please enter an input: ")
#do something with my_input

Note that the input is always a string. To retrieve a number, you can use the built-in int() function:

my_input = int(raw_input("Please enter an input: "))
#do something with my_input

As one other answer mentioned, this will throw an error if the input is a float.

There's also another function, input, in Python 2.x. However, in this version of Python, input evaluates the input, which is a bad idea. It's not recommended to use it.

For Python 3.x, however, you can use the input() function without any problem, since it's a replacement for raw_input:

my_input = input("Please enter an input: ")
#do something with my_input

Taking input from command line arguments

You can also retrieve your input from command line arguments, when executing your script like this:

$ python my_script.py arg1 arg2

The arguments will be stored in the list sys.argv. sys.argv[0] is the first argument, sys.argv[1] is the second argument, and so on.
Example:

import sys
my_input = sys.argv[0]
#do something with my_input

See the details of it here This method works for both versions, Python 3.x and 2.x .

Hope this helps!

Solution 2

One of the ways is: http://docs.python.org/2/library/functions.html#raw_input

For something more advanced: http://docs.python.org/2/library/cmd.html

Solution 3

The best way to take input for a user is using raw_input, this will take in user input as a string. Let me demonstrate:

>>> var = raw_input("Enter")
Enter>? happy
>>> var
'happy'

Notice the quote-marks on happy, this indicates a string. You may also notice, input, and yes that can be used to take user input, but here is one example where that is a bad idea:

>>> a = 2
>>> input("Enter")
Enter>? a+1
3

Here, input actually gets evaluated, since we've already declared a, a + 1 == 3, and we see that as the output in our console session. This later becomes a security concern (you would not want users messing around with your variables), so for user input, raw_input is the best choice.

Since you get a string from raw_input, you can convert it to whatever you like, if it can be converted, for example:

>>> var = raw_input("Enter")
Enter>? 122
>>> var = int(var)
>>> var
122

However, floats will not work with int:

>>> int('1.223')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '1.223'

You will need to use float here, then it works:

>>> float('1.223')
1.223

Solution 4

You can use either input or raw_input functions.

Note: input is prone to security issues, so use raw_input instead.

For example:

inputString = raw_input()

this will read the input line and store it in inputString. If you want an int,

inputInt = int(raw_input())
Share:
18,808
Lokesh
Author by

Lokesh

I am a two eyed creature with no tail.

Updated on November 25, 2022

Comments

  • Lokesh
    Lokesh over 1 year

    I am taking part in a competition, but have no idea how to make python take the input. Here's a typical example . The site has given example of how to take input but those are using C and Java ( click here ). Please help me figure out how to make python take the input in this case.

    • Ashwini Chaudhary
      Ashwini Chaudhary over 10 years
      Either of these will work fine on codechef.