Python_Output_Input
Python_Output_Input
Good Morning!
It is rainy today
In the above example, the print() statement only includes the object to be printed. Here, the
value for end is not used. Hence, it takes the default value '\n'.
Notice that we have included the end= ' ' after the end of the first print() statement.
In [3]: print('New Year', 2023, 'See you soon!', sep= '. ')
In the above example, the print() statement includes multiple items separated by a comma.
Notice that we have used the optional parameter sep= ". " inside the print() statement.
name = "WeSchool"
# print literals
print(5)
# print variables
print(number)
print(name)
5
-10.6
WeSchool
1 of 3 01-03-2024, 13:12
Python Output Input http://localhost:8888/nbconvert/html/Archana%20Python/Python/Pyth...
WeSchool is awesome.
Output formatting
Sometimes we would like to format our output to make it look attractive. This can be done
by using the str.format() method. For example,
In [6]: x = 5
y = 10
Here, the curly braces {} are used as placeholders. We can specify the order in which they are
printed by using numbers (tuple index).
Python Input
While programming, we might want to take the input from the user. In Python, we can use
the input() function.
Syntax of input()
input(prompt)
Enter a number: 5
You Entered: 5
Data type of num: <class 'str'>
In the above example, we have used the input() function to take input from the user and
stored the user input in the num variable.
It is important to note that the entered value 10 is a string, not a number. So, type(num)
returns <class 'str'>.
To convert user input into a number we can use int() or float() functions as:
2 of 3 01-03-2024, 13:12
Python Output Input http://localhost:8888/nbconvert/html/Archana%20Python/Python/Pyth...
Enter a number: 5
You Entered: 5
Data type of num: <class 'int'>
In [ ]:
3 of 3 01-03-2024, 13:12