---
title: "Python Input : How to Accept User Input to Python Scripts"
description: "Learn in this tutorial all the basics of accepting Python input from users by running commands in the terminal, storing variables, and writing input to text files."
canonical: "https://adamtheautomator.com/python-input/"
---

# Python Input : How to Accept User Input to Python Scripts

> Learn in this tutorial all the basics of accepting Python input from users by running commands in the terminal, storing variables, and writing input to text files.

Source: https://adamtheautomator.com/python-input/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![Python Input : How to Accept User Input to Python Scripts](https://adamtheautomator.com/wp-content/uploads/2021/09/Python-Input-How-to-Accept-User-Input-to-Python-Scripts.jpg)

# Python Input : How to Accept User Input to Python Scripts

[![](https://secure.gravatar.com/avatar/26d98f5933e33a53463dd6b5bd002cbae84eb2e0b5c1f47aa8b452597c3f0f74?s=192&d=mm&r=g)Helen Mary Barrameda](https://adamtheautomator.com/author/helenmary-barrameda/)24 September 20214 min. read

Categories: [IT Ops](/category/it-ops/)

Tags:[Python](/tag/python/)

Table of Contents

*   [Prerequisites](#prerequisites)
*   [Getting Interactive Python Input in the Shell](#getting-interactive-python-input-in-the-shell)
*   [Storing User Inputs in a Variable](#storing-user-inputs-in-a-variable)
*   [Writing User Input to a Text File](#writing-user-input-to-a-text-file)
*   [Conclusion](#conclusion)

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.

## Prerequisites

This tutorial comprises hands-on demonstrations. If you’d like to follow along, be sure you have the following:

*   [Python 3.9.1](https://www.python.org/downloads/) 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()`](https://devdocs.io/python~3.9/library/functions#input) command. The `input()` command allows you to require a user to enter a string or number while a program is running.

_The `input()`method replaced the old [`raw_input()`](https://www.python.org/dev/peps/pep-3111/) method that existed in Python v2._

Open a terminal and run the `python` command to access Python.

```python
python
```

You can tell from the prompt shown below (**\>>>**) that you now have access to Python.

![Accessing Python](https://adamtheautomator.com/wp-content/uploads/2021/09/image-192.png)

Accessing Python

Now run the `input()` commands below, one after another. Doing so will require a user to input a text and a number.

```python
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.

![Running Sample input() Commands](https://adamtheautomator.com/wp-content/uploads/2021/09/image-193.png)

Running Sample `input()` Commands

## 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](https://www.pluralsight.com/guides/python-basics-variables-assignment).

To demonstrate storing user input in variables:

Run each command below to require a user to input a number, then store that input in the `age` variable. The [`type()`](https://devdocs.io/python~3.9/library/functions#type) command then returns the [data t](https://press.rebus.community/programmingfundamentals/chapter/data-types/)[ype](https://press.rebus.community/programmingfundamentals/chapter/data-types/) the `age` variable holds.

```python
# 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](https://pythonexamples.org/python-type-casting/) the `input()` command. Typecasting is a process to convert the variable data type into a specific data type.

> _The `input()` command automatically converts a user input to a string unless you [typecast](https://www.geeksforgeeks.org/type-casting-in-python-implicit-and-explicit-with-examples/) the input to another form, such as an integer._

![Executing and Typecasting an input() Command](https://adamtheautomator.com/wp-content/uploads/2021/09/image-194.png)

Executing and Typecasting an `input()` Command

> _What if you expected a number for the `age` variable, but the user entered a string or a phrase? In that case, you need to apply [error handling methods](https://docs.python.org/3/tutorial/errors.html) 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](https://docs.python.org/3/tutorial/inputoutput.html) in reading and writing text files in Python_.

```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 `input()` commands with `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.

```python
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._

![Need a text file name](https://adamtheautomator.com/wp-content/uploads/2021/09/image-195.png)

Need a text file name

4\. Finally, view the [text file](https://adamtheautomator.com/python-read-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 `content_of_file` variable.

Related:[How to Open, Read and Write Text files in Python \[With Examples\]](https://adamtheautomator.com/python-read-file/)

![Sample Text File Generated from Python Input Script](https://adamtheautomator.com/wp-content/uploads/2021/09/image-196.png)

Sample Text File Generated from Python Input Script

## Conclusion

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](https://pandas.pydata.org/) 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?

Related:[Understanding Python Loops and Flow Control for Newbies](https://adamtheautomator.com/python-loop/)

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fpython-input%2F&text=Python%20Input%20%3A%20How%20to%20Accept%20User%20Input%20to%20Python%20Scripts)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fpython-input%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fpython-input%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2026/02/featured_image-3.webp)

### [How to Ace the Modern Coding Interview as a SysAdmin](/ace-modern-coding-interview-sysadmin/)

Master coding interviews for sysadmin and DevOps roles with practical preparation strategies. Learn Python, Bash, and platform-specific techniques that translate your operational experience into interview success.

![](https://adamtheautomator.com/wp-content/uploads/2023/11/pytorch.jpg)

### [How to Install PyTorch on Window](/pytorch/)

Embark on this journey and unleash PyTorch’s potential — your gateway to machine learning and AI exploration, through this ATA Learning tutorial!

![](https://adamtheautomator.com/wp-content/uploads/2023/08/install-python-macos.jpg)

### [Get Started with Programming and Install Python on macOS](/install-python-on-macos/)

Learn how to get started with programming and automation and install Python on macOS in this ATA Learning tutorial!

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
