How do I read from stdin?
You can read from stdin
in Python using the input
function.
Here's an example:
# Read a line from stdin and store it in a variable
input_str = input()
# Print the contents of the variable
print(input_str)
Watch a video course
Python - The Practical Guide
You can also use the input
function to prompt the user for input. For example:
# Prompt the user for their name
name = input("What is your name? ")
# Print a greeting to the user
print(f"Hello, {name}!")
Note that the input
function reads input as a string, so you may need to convert the input to a different data type (e.g., int, float) if necessary.