Output and Input in Python

Reading Time: 3 Minutes
Publication date:

To output data in Python, we use the print() function. To take user input, we use the input() function.

Output in Python

We can directly use the print() function to output data in Python. For example:

print('Python is powerful')
# Result: Python is powerful

Here, the print() function outputs the string provided in single quotes.

The syntax of the print() function:

print(object=, sep=, end=, file=, flush=)

The print() function can have five parameters:

  • object: The value(s) to be printed.
  • sep (optional): Allows separating multiple objects inside the print() function.
  • end (optional): Allows adding a special value, such as a newline \n or a tab \t.
  • file (optional): Specifies the location to output the data. By default, it uses sys.stdout (the screen).
  • flush (optional): A boolean that specifies whether to flush the output or buffer it. Defaults to False.

Example #1: print() Function with One Parameter

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

Output:

Good Morning!
It is rainy today

Here, the print() function has only one parameter – the object to output. Since the end parameter is not specified, the default value \n is used. Therefore, we get the output on two separate lines.

Example #2: print() Function with end Parameter

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

Output:

Good Morning! It is rainy today

Notice that we added the end=' ' parameter to the print() function. As a result, we get the output on one line, separated by a space.

Example #3: print() Function with sep Parameter

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

Output:

New Year. 2024. See you soon!

Here, multiple items are used in the print() function, separated by commas. Notice that we also added the sep='. ' optional parameter, which separates the items with a period instead of a comma.

Example #4: Outputting Variables and Literals in Python

The print() function can also be used to output variables. For example:

number = -11.7
name = "Devinstructor"
# Printing literal
print(7)
# Printing variables
print(number)
print(name)

Output:

7
-11.7
Devinstructor

Example #5: Outputting Concatenated Strings

We can also concatenate two strings inside the print() function. For example:

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

Output:

Ravesli is awesome.

Here, the + operator concatenates the two strings 'Ravesli is ' and 'awesome.', and the print() function outputs the result on the screen.

Example #6: Outputting Formatted Text

Sometimes, we need to format the output to make it look attractive. This can be done with the str.format() method. For example:

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

Output:

The value of x is 6 and y is 12

Here, braces {} are used as placeholders.

Input in Python

To take input from the user in Python, we use the input() function.

The syntax of the input() function:

input(prompt)

Here, prompt is an optional string that is printed to the screen.

See an example of taking input from the user in Python:

# Uses the input() function to take user input
num = input('Enter a number: ')
print('You Entered:', num)
print('Data type of num:', type(num))

Output:

Enter a number: 11
You Entered: 11
Data type of num: <class 'str'>

Here, we used the input() function to get the user input and save it in the variable num.

Importantly, the value 11 provided by the user is a string, not a number. Therefore, type(num) returns <class 'str'>.

To convert user input to a number, we can use the int() or float() function like this:

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

Here, the data type of the user input is converted from a string to an integer.

5 / 5. 1

Share:

Leave a comment