Skip to content

Input and output

Content summary

This lesson explains how to read input and display output in Python.

Overview

In programming, input means the program receives data from the user, while output means the program displays information on the screen.


Reading input

Input in Python

To let the user enter data into a program, we use the built-in function input().

Illustration of the input() function

Example:
Lines 2 and 3 prompt the user to enter their username and password.

1
2
3
if __name__ == '__main__':
    username = input('Tên đăng nhập: ')
    password = input('Mật khẩu: ')

Running the code above produces the following result:

Tên đăng nhập: mrschool
Mật khẩu: 12345678

Displaying output

Output in Python

To display information on the screen, we use the print() function.

Illustration of the print() function

From now on, we will simply say: "print to the screen" or just "print".

Example:
Lines 5 and 6 print the username and password that the user just entered.

1
2
3
4
5
6
if __name__ == '__main__':
    username = input('Tên đăng nhập: ')
    password = input('Mật khẩu: ')

    print(f'Tên đăng nhập của bạn là {username} và mật khẩu là {password}')
    print('Bạn đã bị hack 😆')

Running the code above produces the following result:

Tên đăng nhập: mrschool
Mật khẩu: 12345678
Tên đăng nhập của bạn là mrschool và mật khẩu là 12345678
Bạn đã bị hack 😆

f-string

f-string = formatted string.

An f-string lets you insert variables directly into a string using this syntax:

  • Put the letter f before the string: f'...'
  • Place the variable inside curly braces {variable_name}

Using f-strings with print() is the modern, professional, and readable way in Python.


Source code

The complete code is available at:


Summary mindmap


Some English words

Vietnamese Tiếng Anh
dữ liệu đầu vào input
thông tin đầu ra output