Print and Input Functions¶
Let us understand details related to print
and input
functions. These functions will help us in enhancing our learning experience.
print
is primarily used to print the strings on the console whileinput
is used to accept the input from console.input
will typically prompts the user to enter the value and we can assign to a variable and use it further.By default, the value passed for
input
is of typestr
and hence we need to convert the data type of the values entered before processing.
input?
Signature: input(prompt='')
Docstring:
Forward raw_input to frontends
Raises
------
StdinNotImplentedError if active frontend doesn't support stdin.
File: /opt/anaconda3/envs/beakerx/lib/python3.6/site-packages/ipykernel/kernelbase.py
Type: method
a = int(input('Enter first value: '))
b = int(input('Enter second value: '))
print(f'Sum of first value {a} and second value {b} is {a + b}')
Enter first value: 4
Enter second value: 5
Sum of first value 4 and second value 5 is 9
When we use
print
with one string, by default it will have new line character at the end of the srring.We can also pass multiple strings (varrying arguments) and space will be used as delimiter
We can change the end of the string character, by passing argument
print
can accept 2 keyword argumentssep
andend
to support the above mentioned features.
You will understand more about keyword arguments and varrying arguments in User Defined Functions.
print?
# Default print by using one string as argument
print('Hello')
print('World')
Hello
World
# Default print by using multiple arguments. Each argument is of type string
print('Hello', 'World')
Hello World
# Changing end of line character - empty string
print('Hello', end='')
print('World')
HelloWorld
# Changing separater to empty string
print('Hello', 'World', sep='')
HelloWorld
print('Hello', 'World', sep=', ', end=' - ')
print('How', 'are', 'you?')
Hello, World - How are you?