Python language basics 12: reading user input

Introduction

In the previous post we looked at how to construct a while-loop and what its purpose is. In this post we’ll quickly learn how read a user’s input.

As a reminder I use the free edition of the PyCharm IDE so all the printouts will be from that environment.

Reading user input

We can use the “input” method to read the user’s input. It comes in two versions. The first version simply waits for the user to enter something in the console without a question:

promptLessInput = input()
print('Your promptless input was: ' + promptLessInput)

If you run this code in PyCharm then at first it may not be obvious what’s happening. You’ll need to click in the output window, type something and press Enter:

Promptless input in Python

Another version of input – an overloaded version – accepts a prompt, which can simply be a question. Consider the following example:

promptedInput = input('What have you got to say: ')
print('Your prompted input is: ' + promptedInput)

If you’re new to programming then you probably won’t understand what “overloaded” means but you can safely ignore it for now.

If you run this code then you’ll see the question in the output window. You’ll need to click at a position after the question, type some input and press Enter.

Prompted input in Python

So, it’s that easy to catch the user’s input in a console window in Python.

Read all Python-related posts on this blog here.

Advertisement

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

One Response to Python language basics 12: reading user input

  1. Brian Dead Rift Webb says:

    Reblogged this on Brian By Experience.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Elliot Balynn's Blog

A directory of wonderful thoughts

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

WEB APPLICATION DEVELOPMENT TUTORIALS WITH OPEN-SOURCE PROJECTS

Once Upon a Camayoc

Bite-size insight on Cyber Security for the not too technical.

%d bloggers like this: