Skip to content

RESOURCES / BLOG

How Does Python User Input Work (and Why Should You Care)?

Hey all,

I’m learning Python for some personal projects. I’d like to make my script more flexible so I can input things like the image file name or transformation settings without hardcoding them.

I keep seeing examples of using Python user input, but I don’t totally get how it works or how to use it properly in a real project. Could someone walk me through it?

Thanks in advance!

Props to you for wanting to make your scripts more dynamic! Using Python user input is one of the simplest but most powerful ways to make your code interactive. It allows your program to respond to instructions from the user at runtime instead of relying on hardcoded values.

Let’s break it down and walk through how it works, including how it can be used with something like the Cloudinary Python SDK.

In short, user input allows you to ask the user to type something while the program is running.

input("Enter something: ")

It’s important to remember to add a space at the end of the string, because it makes the prompt for the user more readable. Whatever the user types will be stored as a string, and you can assign that to a variable:

name = input("What's your name? ")

print("Hello,", name)Code language: PHP (php)

If you run this script, Python will pause at the input() line and wait for the user to type something.

Let’s say you’ve been uploading the same image over and over. Instead, let’s allow the user to input the filename at runtime.

import cloudinary

import cloudinary.uploader

# Configure Cloudinary

cloudinary.config(

  cloud_name = "your-cloud-name",

  api_key = "your-api-key",

  api_secret = "your-api-secret"

)

# Ask user to enter the filename

file_name = input("Enter the image filename (e.g. image.jpg): ")

# Upload the image

result = cloudinary.uploader.upload(file_name)

# Print the resulting URL

print("Image uploaded successfully!")

print("URL:", result["secure_url"])Code language: PHP (php)

Now you can upload any image just by typing the file name when prompted, no code edits needed!

Remember: input() always returns a string. If you want to do math or compare numeric values, convert the input:

age = int(input("Enter your age: "))

if age >= 18:

    print("You're an adult.")

else:

    print("You're a minor.")Code language: PHP (php)

Sometimes users type with extra spaces or weird characters. You can clean up their input like this:

filename = input("File name: ").strip()

Want to provide a default if they enter nothing?

file_name = input("Enter file name (default: sample.jpg): ").strip()

if not file_name:

    file_name = "sample.jpg"Code language: JavaScript (javascript)

Want to let the user upload multiple files?

while True:

    file_name = input("Enter image file (or 'q' to quit): ").strip()

    if file_name.lower() == 'q':

        break

    result = cloudinary.uploader.upload(file_name)

    print("Uploaded:", result["secure_url"])Code language: PHP (php)

This makes your script feel like a command-line tool. Just don’t forget to add a way to end the loop!

User input can be unpredictable. You should validate it when possible:

while True:

    width = input("Enter width in pixels: ")

    if width.isdigit():

        width = int(width)

        break

    else:

        print("Please enter a valid number.")Code language: PHP (php)

This helps avoid crashes because of bad data.

You can use user input to dynamically resize or crop images before uploading:

transform = input("Enter transformation (e.g. w_300,h_300,c_fill): ")

result = cloudinary.uploader.upload(

    "photo.jpg",

    transformation=[{

        "width": 300,

        "height": 300,

        "crop": "fill"

    }]

)

print("Transformed image:", result["secure_url"])Code language: PHP (php)

You could even let the user type in values for width, height, or crop mode.

FeatureDescription
input()Reads text typed by the user
Always returns strConvert with int(), float(), etc.
Use .strip()Removes whitespace or formatting issues
Combine with loopsMake repeatable tasks (like batch uploads)
Add validationPrevent errors from bad input
Integrates with SDKsLike Cloudinary for dynamic media workflows

Adding Python user input to your scripts is a great way to take control out of the code editor and put it into the hands of the user, even if that user is just you.Whether you’re asking for a file name, a transformation, or custom image metadata, input() makes your Python scripts more flexible, reusable, and fun to work with. It’s especially useful for media workflows involving Cloudinary, where image details might change from run to run.

Start Using Cloudinary

Sign up for our free plan and start creating stunning visual experiences in minutes.

Sign Up for Free