In Python, there are multiple ways to read users inputs. One of the most common way to take input from the user using the input() function. Below i will explain, the most common ways to read users input.
Command Line Input (Interactive):
Here is a simple example of how to use the input()
function to ask the user for their name and print a personalized greeting: The input() function reads a line of text from the standard input (usually the keyboard) and returns it as a string.
name = input("What is your name? ") print("Hello, " + name + "!")
When you run this program, it will display the message “What is your name?” on the screen and wait for the user to enter their name. Once the user has entered their name and pressed Enter, the program will print a personalized greeting. You can also use the input() function to ask the user for numerical input, like this:
age = input("How old are you? ") age = int(age) print("You will be " + str(age + 1) + " years old next year.")
In this example, the input()
function is used to ask the user for their age. The input is read as a string, so it needs to be converted to an integer using the int()
function before it can be used in arithmetic operations. Finally, the program prints a message that includes the user’s age incremented by 1.

It’s important to note that the input() function always returns a string, so you need to convert the input to the appropriate data type before using it in calculations or comparisons.
You can also use the input()
function in conjunction with loops and conditionals to create interactive programs that respond to user input. Here is an example that uses a loop to ask the user for a series of numbers and prints the sum of those numbers:
sum = 0 while True: num = input("Enter a number (or 'q' to quit): ") if num == 'q': break sum += int(num) print("The sum is " + str(sum))
In this example, the program repeatedly asks the user for a number until the user enters the letter ‘q’. Each time the user enters a number, it is added to a running total. When the user enters ‘q’, the loop exits and the program prints the sum of the numbers.
Command Line Arguments (via sys.argv or argparse):
You can also read input from the command line by passing arguments to the Python script. Here’s an example. In this example, the first command-line argument is stored in the name
variable and used to print a personalized greeting.
import sys name = sys.argv[1] print("Hello, " + name + "!")
Standard Input Redirection / Piping:
sys.stdin.readline()
: This function reads a line from the standard input stream (stdin
) and returns it as a string. Here’s an example: In this example, the strip()
method is used to remove any trailing whitespace from the input.
import sys name = sys.stdin.readline().strip() print("Hello, " + name + "!")
Environment Variables:
Environment variables allow Python applications to access configuration and secret data without hardcoding them into the source code. This is particularly useful for credentials, API keys, or environment-specific settings (like development vs. production). Using os.getenv(), you can safely retrieve environment variables, providing default values if necessary to avoid runtime errors. This method keeps sensitive data secure and makes applications more flexible across different environments.
import os os.getenv('ENV_VAR_NAME')
Reading from Files:
File input is a fundamental way to ingest data in Python, often used for processing structured data like CSV, JSON, or text files. Using the built-in open() function combined with methods like .read(), .readlines(), or iteration, you can efficiently load file contents into your program.
with open('input.txt', 'r') as file: for line in file: print(line.strip())
In this example, the open()
function is used to open the file input.txt
for reading. The with
statement ensures that the file is properly closed when the block of code is finished. The for
loop reads each line of the file and prints it to the screen after removing any trailing whitespace.
Bonus: Input Passwords
getpass.getpass()
: This function prompts the user to enter a password without echoing the characters on the screen. Here’s an example:
import getpass password = getpass.getpass() print("Your password is " + password)
In this example, the getpass()
function prompts the user to enter a password without displaying the characters on the screen. The password is then stored in the password
variable.
Overall, the input()
function is the most commonly used method for reading user input in Python, but the other methods can be useful in specific situations, such as reading sensitive data or reading input from a file.
The codes (if any) mentioned in this post can be downloaded from github.com. Share the post with someone you think can benefit from the information