Input and Output in Python - GeeksforGeeks

18 Aug, 2021
In this article, we will see different ways in which first we can take input from users and second show output to them.
Sometimes a developer might want to take user input at some point in the program. To do this Python provides an input() function.
Syntax:
input('prompt')
where prompt is an optional string that is displayed on the string at the time of taking input.
Python3
# Taking input from the username = input("Enter your name: ") # Outputprint("Hello, " + name)print(type(name))
Output:
Enter your name: GFG
Hello, GFG
<class 'str'>
Note: Python takes all the input as a string input by default. To convert it to any other data type we have to convert the input explicitly. For example, to convert the input to int or float we have to use the int() and float() method respectively.
Python3
# Taking input from the user as integernum = int(input("Enter a number: ")) add = num + 1 # Outputprint(add)
Output:
Enter a number: 25
26
Python provides the print() function to display output to the standard output devices. 
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 printedsep=’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.stdoutflush : (Optional) A Boolean, specifying if the output is flushed (True) or buffered (False). Default: False
Returns: It returns output to the screen.
Python3
# Python program to demonstrate# print() methodprint("GFG") # code for disabling the softspace feature print('G', 'F', 'G')
GFG
G F G

In the above example, we can see that in the case of the 2nd print statement there is a space between every letter and the print statement always add a new line character at the end of the string. This is because after every character the sep parameter is printed and at the end of the string the end parameter is printed. Let’s try to change this sep and end parameter.
Python3
# Python program to demonstrate# print() methodprint("GFG", end = "@") # code for disabling the softspace feature print('G', 'F', 'G', sep="#")
GFG@G#F#G

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: Python String formatting using F string
Python3
# Declaring a variablename = "Gfg" # Outputprint(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: Python string formatting using format() function
Python3
# Initializing variablesa = 20b = 10 # additionsum = a + b # subtractionsub = a- b # Outputprint('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. The formatting using % is similar to that of ‘printf’ in the C programming language.
%d – integer
%f – float
%s – string
%x – hexadecimal
%o – octal
Example:
Python3
# Taking input from the usernum = int(input("Enter a value: ")) add = num + 5 # Outputprint("The sum is %d" %add)
Output:
Enter a value: 50
The sum is 55
sweetyty
nikhilaggarwal3
python-input-output
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Read a file line by line in Python
How to Install PIP on Windows ?
Enumerate() in Python
Different ways to create Pandas Dataframe
Iterate over a list in Python
Python String | replace()
*args and **kwargs in Python
Reading and Writing to text files in Python
Create a Pandas DataFrame from Lists