Python Basic Input and Output

Python Output

In Python, we can simply use the print() function to print output. For example,

print('Python is powerful')

# Output: Python is powerful

Here, the print() function displays the string enclosed inside the single quotation.

Syntax of print()

In the above code, the print() function is taking a single parameter.

However, the actual syntax of the print function accepts 5 parameters

print(object= separator= end= file= flush=)

Here,

  • object - value(s) to be printed
  • sep (optional) - allows us to separate multiple objects inside print().
  • end (optional) - allows us to add add specific values like new line "\n", tab "\t"
  • file (optional) - where the values are printed. It's default value is sys.stdout (screen)
  • flush (optional) - boolean specifying if the output is flushed or buffered. Default: False

Example 1: Python Print Statement

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

Output

Good Morning!
It is rainy today

In the above example, the print() statement only includes the object to be printed. Here, the value for end is not used. Hence, it takes the default value '\n'.

So we get the output in two different lines.


Example 2: Python print() with end Parameter

# print with end whitespace
print('Good Morning!', end= ' ')

print('It is rainy today')

Output

Good Morning! It is rainy today

Notice that we have included the end= ' ' after the end of the first print() statement.

Hence, we get the output in a single line separated by space.


Example 3: Python print() with sep parameter