User Input
Python allows programs to interact with users by accepting user input. The primary tool for this is the input() function, and Python also supports command-line input using the sys.argv list.
Getting User Input
Python provides an easy way to get user input using the input() function.
It pauses the program and waits for the user to type something and press Enter.
Example:
name = input("Please enter your name: ")
x = input("Enter first number: ")
y = input("Enter second number: ")
z = x + y
print(z)Output:
Enter first number: 9
Enter second number: 5
95Note: The output is
95because theinput()function returns a string by default.
How the input() Function Works
The input() function accepts user input from the command line.
name = input("Enter your name: ")
print(name)value = input("Enter something: ")
print(value)The input() function always returns a string. Even if you enter 10, it is treated as "10".
If you want to use the input as a number, convert it using int() or float().
Type Conversion
Since all input values are returned as strings, we must convert them to perform arithmetic.
Example:
x = input("Enter first number: ")
y = input("Enter second number: ")
a = int(x)
b = int(y)
z = a + b
print(z)Output:
Enter first number: 9
Enter second number: 5
14Instead of creating extra variables, we can simplify this:
x = int(input("Enter first number: "))
y = int(input("Enter second number: "))
z = x + y
print(z)Using Index Value
You can access a single character from user input using indexing.
ch = input("Enter a character: ")
print(ch[0])Output:
Enter a character: Muskan
MDirectly access a single character through the input:
ch = input("Enter a character: ")[0]
print(ch)Output:
Enter a character: Muskan
MThe eval() Function
The eval() function evaluates the input as a Python expression and returns the result.
Example1:
x = eval(input("Enter an expression: "))
print(type(x))Output:
Enter an expression: 9 + 5
<class 'int'>Example2:
result = eval(input("Enter an expression: "))
print(result)Output:
Enter an expression: 2 + 3 * 5
17Command-Line Arguments
Python’s sys module allows passing values from the command line.
Use sys.argv to access the command-line arguments.
Example (Mycode.py)
import sys
x = sys.argv[1]
y = sys.argv[2]
z = x + y
print(z)Command:
python Mycode.py 9 5Output:
95Here:
Mycode.py→ Argument 09→ Argument 15→ Argument 2
Since these are strings, the result is concatenation.
To fix this, convert inputs to integers:
import sys
x = int(sys.argv[1])
y = int(sys.argv[2])
z = x + y
print(z)Command:
python Mycode.py 9 5Output:
14Summary
| Concept | Description | Example |
|---|---|---|
input() | Takes user input as a string | x = input("Enter name: ") |
| Type Conversion | Converts string input to number | int(input("Enter number: ")) |
| Indexing | Access specific character | input("Enter: ")[0] |
eval() | Evaluates an expression | eval(input("Enter expr: ")) |
| Command-line Input | Takes arguments via terminal | sys.argv |
Written By: Muskan Garg
How is this guide?
Last updated on
