Skip to content
Related Articles

Related Articles

Improve Article
Input and Output in Python
  • Last Updated : 29 Aug, 2020

In this article, we will see different ways in which first we can take input from users and second show output to them.

Taking Input from the user

Sometimes a developer might want to take input from the user at some point in the program. To do this Python provides an input() function.

Syntax:

input('prompt')

where, prompt is a string that is displayed on the string at the time of taking input.

Example 1: Taking input from the user with a message.



Python3




# Taking input from the user
name = input("Enter your name: ")
  
# Output
print("Hello, " + name)

Output:

Enter your name: Gfg
Hello, Gfg

Example 2: By default input() function takes the user’s input in a string. So, to take the input in the form of int, you need to use int() along with input function.

Python3




# Taking input from the user as integer
num = int(input("Enter a number: "))
  
add = num + 1
  
# Output
print(add)

Output:

Enter a number: 25
26

Displaying Output

Python provides the print() function to display output to the console.

Syntax: print(value(s), sep= ‘ ‘, end = ‘\n’, file=file, flush=flush)

Parameters:
value(s) : Any value, and as many as you like. Will be converted to string before printed
sep=’separator’ : (Optional) Specify how to separate the objects, if there is more than one.Default :’ ‘
end=’end’: (Optional) Specify what to print at the end.Default : ‘\n’
file : (Optional) An object with a write method. Default :sys.stdout
flush : (Optional) A Boolean, specifying if the output is flushed (True) or buffered (False). Default: False

Returns: It returns output to the screen.

Example:

Python3




# Python program to demonstrate
# print() method
print("GFG")
  
# code for disabling the softspace feature  
print('G', 'F', 'G', sep ='') 
    
# using end argument 
print("Python", end = '@')   
print("GeeksforGeeks")

Output:



GFG
GFG
Python@GeeksforGeeks

Formatting Output

Formatting output in Python can be done in many ways. Let’s discuss them below

  • We can use formatted string literals, by starting a string with f or F before opening quotation marks or triple quotation marks. In this string, we can write Python expressions between { and } that can refer to a variable or any literal value.

Example:

Python3




# Declaring a variable
name = "Gfg"
  
# Output
print(f'Hello {name}! How are you?')

Output:

Hello Gfg! How are you?
  • We can also use format() function to format our output to make it look presentable. The curly braces { } work as placeholders. We can specify the order in which variables occur in the output.

Example:

Python3




# Intializing variables
a = 20
b = 10
  
# addition
sum = a + b
  
# subtraction
sub = a- b
  
# Output
print('The value of a is {} and b is {}'.format(a,b))
  
print('{2} is the sum of {0} and {1}'.format(a,b,sum))
  
print('{sub_value} is the subtraction of {value_a} and {value_b}'.format(value_a = a , 
                                                                         value_b = b,
                                                                         sub_value = sub))

Output:

The value of a is 20 and b is 10
30 is the sum of 20 and 10
10 is the subtraction of 20 and 10
  • We can use ‘%’ operator. % values are replaced with zero or more value of elements.

Example:

Python3




# Taking input from the user
num = int(input("Enter a value: "))
  
add = num + 5
  
# Output
print("The sum is %d" %add)

Output:

Enter a value: 50
The sum is 55

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :