100% found this document useful (3 votes)
4K views5 pages

Python Input and Output Statements

The document discusses Python input and output statements. It provides examples of using the input() function to take input from the user and the print() function to output results. It also covers formatting output using formatted string literals and the format() function.

Uploaded by

Narendra Chauhan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
100% found this document useful (3 votes)
4K views5 pages

Python Input and Output Statements

The document discusses Python input and output statements. It provides examples of using the input() function to take input from the user and the print() function to output results. It also covers formatting output using formatted string literals and the format() function.

Uploaded by

Narendra Chauhan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 5

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.

 Python-3 supports only for input() function .

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)

Or else even we can simplify the above program:


x=int(input("Enter Fisrt number:"))
y=int(input("Enter Second Number:"))
print("The Sum:", i+j)
Let us consider another example to know how we can use the input() function. The
Following example read the employee data from the keyboard and print that data.
eno=int(input("Enter the Emplyoyee number:"))
ename=input("Enter the Employee name:")
esal=float(input("Enter the employee salary:"))
eaddr=input("Enter the employee address:")
married=bool(input("Is employee married?[True|False]:"))

print("Please confimr your provided information")


print("Emplyoyee number:", eno)
print("Employee name:", ename)
print("Employee salary:", esal)
print(" Employee address:", eaddr)
print("Employee married?:", married)

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

How to Display Output in Python


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 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: Python Print Output
# Python program to demonstrate

# print() method

print("GFG")

 # code for disabling the softspace feature 


print('G', 'F', 'G')

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

Using formatted string literals

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 variable
name ="Gfg"

 # Output
print(f'Hello {name}! How are you?')

Output: 
Hello Gfg! How are you?

Using format()

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 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

You might also like