W3docs

How do I read from stdin?

You can read from stdin in Python using the input function.

You can read from stdin in Python using the input function.

Here's an example:

Read a line from stdin in Python

# Read a line from stdin and store it in a variable
input_str = input()

# Print the contents of the variable
print(input_str)

<div class="alert alert-info flex not-prose"> Watch a course <span class="hidden md:block">Watch a video course </span> Python - The Practical Guide</div>

You can also use the input function to prompt the user for input. For example:

Read a line from stdin in Python along with a text to promt the user for input

# 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.