Python User Input
Use Python's input()
function to accept user input. You can then use this input within your program.
You can write Python programs that accept user input. You could ask the user their name, their age, or pretty much anything. The user's input can then be used within your program in any number of ways. You could simply print their input back to the screen, you could insert it into a database, or you make the program do different things depending on the input received.
Basic Example
Here's a basic example of using the input()
function:
What is your name?
This prompts the user to provide their name. They can enter it directly into the terminal and see the result.
Once you've captured the user's input, you can print the result out (or do anything else you like with it).
So when you enter this into the terminal, it ends up looking something like this:
>>> name = input("What is your name?") What is your name?Homer >>> print(name) Homer >>>
As with any variable, you can also combine the user's input with other text, like this:
Hello Homer
Python Expressions
The input()
function will evaluate any expression that the user inputs. Here's are some examples: