Python Input / Output Example

This article will tell you how to use input / output in Python by examples.

1. Python Input.

Open a terminal window and input python3 ( MacOS, Linux ), python( Windows ) to enter python interactive environment. If you want to input some character in code and save it to a variable, you can use input() function as below. This function can let user enter a string and store it in a variable.

>>> name = input()

When you type name = input() and press enter, the Python interactive command line will wait for your input character. At this point, you can enter any character and press enter to complete the input. When the input complete, there is no hint, and the Python interactive command line returns to >>> again.

So where is the input we just entered? The answer is that the input string is saved in the name variable. You can simply enter  name variable to see the variable content.

>>> name

To print the contents of the name variable, you can use the print() function in addition to just writing the name and then pressing enter.

>>> print(name)

2. Python Output.

Use the print () combined with strings in parentheses, you can output the specified text to the screen. If you want to output ‘hello world’ on the screen, you can use below code.

>>> print('hello, world')

The print() function can also accept multiple strings, separated by commas “, ” and concatenate them into a single output.

>>> print('Hello', 'Richard', ',Welcome To Python World')
Hello Richard ,Welcome To Python World

Print() prints each string in turn, and when it encounters the comma “, ” it prints a space, so the string is spelled like above. Print() can also be used to print an integer, or calculate the result.

>>> print(1)
>>> print(1 + 1)
>>> print('1 + 1 =', 1 + 1)
1 + 1 = 2

3. Use Python Input Output Together.

With input and output, we can change the program that printed ‘hello, world’ as below.

name = input()
print('hello,', name)

Running the above program, the first line of code allows the user to enter any character as his or her name and then store it in the name variable. The second line of code says hello to the user based on the user’s name which user input.

C:> python
hello, Richard

But when the program is running, there is not any prompt message to tell user to input their name. Fortunately, input() lets you display a string to prompt the user like below.

name = input('Enter your name please : ')
print('hello,', name)

When you run the program again, you will find that when the program runs, it will print out the prompt message first, then after you input your name, it will print ‘hello Richard’ in the output console.

C:> python
Enter your name please: Richard
hello, Richard

Each time the program is running, the output will be different depending on the user’s input.


  1. How To Install Python3 On Windows And MacOS

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.