Python Output Input http://localhost:8888/nbconvert/html/Archana%20Python/Python/Pyth...
Example 1: Python Print Statement
In [1]: print('Good Morning!')
print('It is rainy today')
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'.
Example 2: Python print() with end Parameter
In [2]: # print with end whitespace
print('Good Morning!', end= ' ')
print('It is rainy today')
Good Morning! It is rainy today
Notice that we have included the end= ' ' after the end of the first print() statement.
Hence, we get the output in a single line separated by space.
Example 3: Python print() with sep parameter
In [3]: print('New Year', 2023, 'See you soon!', sep= '. ')
New Year. 2023. See you soon!
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.
Hence, the output includes items separated by . not comma.
Example: Print Python Variables and Literals
In [4]: number = -10.6
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...
Example: Print Concatenated Strings
In [5]: print('WeSchool is ' + 'awesome.')
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
print('The value of x is {} and y is {}'.format(x,y))
The value of x is 5 and y is 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)
Example: Python User Input
In [7]: # using input() to take user input
num = input('Enter a number: ')
print('You Entered:', num)
print('Data type of num:', type(num))
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...
In [9]: # using input() to take user input
num = int(input('Enter a number: '))
print('You Entered:', num)
print('Data type of num:', type(num))
Enter a number: 5
You Entered: 5
Data type of num: <class 'int'>
In [ ]:
3 of 3 01-03-2024, 13:12