Python Input-Output

Displaying Output with print()

Use the print() function to show text or values on the screen.

print('Python is powerful')

Output:

Python is powerful

Here, the string inside the quotes is displayed directly in the terminal.


The Full Syntax of print()

The print() function can take up to five parameters:

print(object, sep=' ', end='\n', file=sys.stdout, flush=False)

Here's what each one means:

  • object: The value(s) to be printed
  • sep (optional): String inserted between multiple values
  • end (optional): What to print at the end (default is a newline \n)
  • file (optional): Where to output (default is the screen)
  • flush (optional): Whether to flush the output buffer (default is False)

Example: Basic print() usage

print('Good Morning!')
print('It is rainy today')

Output:

Good Morning!
It is rainy today

Each print() ends with a newline by default.


Example: Custom end Parameter

print('Good Morning!', end=' ')
print('It is rainy today')

Output:

Good Morning! It is rainy today

By setting end=' ', the second message appears on the same line.


Example: Custom sep Parameter

print('New Year', 2023, 'See you soon!', sep='. ')

Output:

New Year. 2023. See you soon!

The sep='. ' separates each printed item with a period and space instead of the default space.


Example: Printing Variables and Values

number = -10.6
name = "Programiz"

print(5)
print(number)
print(name)

Output:

5
-10.6
Programiz

You can print both literal values and variables.


Example: Concatenating Strings

print('Python is ' + 'awesome.')

Output:

Python is awesome.

Here, + joins the two strings before printing.


Example: Formatted Output with str.format()

x = 5
y = 10
print('The value of x is {} and y is {}'.format(x, y))

Output:

The value of x is 5 and y is 10

You can insert variables into strings using {} placeholders.


Getting User Input with input()

Use the input() function to ask the user for information.

num = input('Enter a number: ')
print('You entered:', num)
print('Data type of num:', type(num))

Example Interaction:

Enter a number: 10
You entered: 10
Data type of num: <class 'str'>

Even if the user types a number, the value returned by input() is always a string.


Converting Input to Numbers

To work with numbers, you need to convert the string:

num = int(input('Enter a number: '))

Now num will actually be an integer.


Summary

  • Use print() to display values. Customize output using sep, end, and str.format().
  • Use input() to collect data from the user. Always convert it if you need numbers.
  • These two functions are essential for any interactive Python program.

Continue Learning

Python Variables

Popular

### Understanding Variables and Literals in Python Variables and literals are the foundation of any

Python Functions Scope

For You

### Understanding Variable Scope In Python, **variable scope** determines where a variable is acces

Python Functions

For You

Functions are blocks of reusable code designed to perform a specific task. They allow you to organiz

Personalized Recommendations

Log in to get more relevant recommendations based on your reading history.