If you’re new to the Python world and wondering how to accept user input in your Python scripts, then you’re at the right spot. Bringing interactivity to your Python scripts is a great way to receive input.
In this tutorial, you’ll learn some popular ways to get user input for your Python scripts.
Table of Contents
This tutorial comprises hands-on demonstrations. If you’d like to follow along, be sure you have the following:
- Python 3.9.1 or later
Getting Interactive Python Input in the Shell
Perhaps you’re working on a program that requires users’ input, such as their age so that they can run the program. If so, you’ll need to use the
input() command. The
input() command allows you to require a user to enter a string or number while a program is running.
input()method replaced the old
raw_input() method that existed in Python v2.
Open a terminal and run the
python command to access Python.
You can tell from the prompt shown below (>>>) that you now have access to Python.
Now run the
input() commands below, one after another. Doing so will require a user to input a text and a number.
input("Insert Your Text Prompt Here:") input("Enter your age: ")
Below, you can see each command accepts the inputs and prints them on the terminal.
Storing User Inputs in a Variable
What if you’re working on a project and need to store user input for later? You need to store user input in variables.
To demonstrate storing user input in variables:
# Getting User Input as String # Stores the user input as string in the 'age' variable age = input("What's your age?") # This command tells you that the input was stored as a string type(age) # Type Casting - Converts string to integer # Stores the user input as an integer in the 'age' variable age = int(input("What's your age?")) # This time, you'll see the user input is stored as an integer type(age)
In the first command below, you can see that the prompt asks the user to input a number (age) but stores the input as a string.
Notice the second command converts the user input from a string to an integer after typecasting the
input() command. Typecasting is a process to convert the variable data type into a specific data type.
input()command automatically converts a user input to a string unless you typecast the input to another form, such as an integer.
What if you expected a number for the
agevariable, but the user entered a string or a phrase? In that case, you need to apply error handling methods or perform some conditional logic to check that the user input a valid number.
Writing User Input to a Text File
Perhaps you’d like to accept Python user input and write that input to a text file. Let’s demonstrate doing just that and do it via a Python script rather than using the console.
1. Create a folder named C:/MyScript. This folder is where you’ll save your Python script.
2. Open your preferred text editor and copy/paste the code below. Save the code as a Python script file (.py) with the name of your choice. But for this example, the text file is named text-writing.py.
Running the code below asks a user for a text file name and its content, then writes it in that text file. The example below is beginner-friendly since Python has built-in functions in reading and writing text files in Python.
name_of_file = input("What is your desired filename for the textfile?") # Display to collect the user input print(name_of_file) content_of_file = input("What would you like to store on this file?") # Ask user what the file must store once it's created print(content_of_file) # The format is open(filename, mode) and in this case, opens name_of_file in write mode f = open(name_of_file, 'w') # Write content_of_file input in file object f from previous command. f.write(content_of_file) # Close the file object after encoding the content_of_file string f.close()
Note that you must run the code on Python 3. If you are using Python 2, replace all
raw_input()for the code to work.
3. Now run the commands below to change the directory to where you saved the text-writing.py script and execute that script.
import os # Import the OS module os.chdir('C:/MyScript') # Change directory to C:\MyScript python text-writing.py # Execute the text-writing.py script
Below, you can see that the script requires user input for a text file’s name and contents to store in that text file. You can name the text file anything you prefer, but call the text file sample-textfile.txt for this example.
4. Finally, view the text file (sample-textfile.txt) created by the script.
As you see below, the sample-textfile.txt file’s content is the same as the user input value from the
Throughout this tutorial, you’ve learned the basics of accepting Python input from users by running commands in the terminal, storing variables, and writing input to text files.
Further extend your newfound knowledge by using dedicated Python libraries like pandas for Excel, CSV, JSON, and other flat files. Why not incorporate stored user inputs in functions or make it a part of your loops or flow control?
More from Adam The Automator & Friends
Expose REST APIs, schedule jobs, and provide custom user interfaces with the platform built for PowerShell to integrate with anything.
We’ve put together a list of the resources we, at ATA, can wholeheartedly recommend!
Why not write on a platform with an existing audience and share your knowledge with the world?