Python Input and Output Statements
Python Input and Output Statements
Input : Any information or data sent to the computer from the user through the
keyboard is called input.
User can be enter tha dta by using the keyboard is called input and compuer gives
the result from user is called output.
Output: The information produced by the computer to the user is called output.
Python provides us with the two inbuilt functions as input() and print().
The syntax for input is input(prompt_message); the python will automatically
identify whether the user entered a string, number, or list; if the input entered from
the user is not correct, then python will throw a syntax error.
And the syntax for the output in python is print(), normally used to print the
output.
The following example will read two inputs from the keyboard and print the sum
x=input("Enter First number:")
y=input("Enter Second Number:")
i=int(x)
j=int(y)
print("The Sum:", i+j)
Python3
# Taking input from the user
name =input("Enter your name: ")
# Output
print("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.
Example 2: Integer input in Python
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
# print() method
print("GFG")
Output
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.
Example: Python Print output with custom sep and end parameter
Python3
# Python program to demonstrate
# print() method
print("GFG", end ="@")
# code for disabling the softspace feature
print('G', 'F', 'G', sep="#")
Output
GFG@G#F#G
Formatting Output
Formatting output in Python can be done in many ways. Let’s discuss them
below
# Output
print(f'Hello {name}! How are you?')
Output:
Hello Gfg! How are you?
Using format()
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