Live AI Powered DevOps with AWS
PythonInput and Swapping Basics

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
95

Note: The output is 95 because the input() 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
14

Instead 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
M

Directly access a single character through the input:

ch = input("Enter a character: ")[0]
print(ch)

Output:

Enter a character: Muskan
M

The 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
17

Command-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 5

Output:

95

Here:

  • Mycode.py → Argument 0
  • 9 → Argument 1
  • 5 → 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 5

Output:

14

Summary

ConceptDescriptionExample
input()Takes user input as a stringx = input("Enter name: ")
Type ConversionConverts string input to numberint(input("Enter number: "))
IndexingAccess specific characterinput("Enter: ")[0]
eval()Evaluates an expressioneval(input("Enter expr: "))
Command-line InputTakes arguments via terminalsys.argv

Written By: Muskan Garg

How is this guide?

Last updated on