Two built-in functions in Python are used to perform input and output operations (OI Operations). The two built-in functions for performing output and input operations are listed below.
- print() – This function is used to output data.
- input() – This function is used to perform input operations.
Take Input from the User:
To perform input operations, Python includes the function input(). The input() function can be used both with and without a message.
where prompt is an optional string displayed on the string when taking input.
# Taking input from the user name = input("Enter your Name: ") print("Hello, " + name) print(type(name))
Enter your Name: Rabecca Fatima Hello, Rabecca Fatima <class 'str'>
By default, Python treats all input as a string. To convert it to another data type, we must explicitly convert the input. To convert an int or a float, for example, use the int() and float() methods, respectively.
# Taking input from the user Roll = int(input("Enter your Roll: ")) print( Roll) print(type(Roll))
Enter your Roll: 20012 20012 <class 'int'>
Output Operation using print() :
The built-in function print( ) is used to output the given data to the standard output device (Screen). When we use the print() function to display a message, the string can be enclosed in single or double quotations.
Note: the return value of print( ) function is None.
print(value(s), sep= ‘ ‘, end = ‘\n’, file=file, flush=flush)
- value(s) : Any value, and as many as you like. Will be converted to a string before getting 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.
Roll = 102 print( "ROLL NUMBER :", Roll)
ROLL NUMBER : 102
Using % Operator:
We can implement the ‘%’ operator. % values are replaced with zero or more element values. % formatting is similar to ‘printf’ formatting in the C programming language.
- %d is an integer,
- %f is a float,
- %s is a string,
- %x is hexadecimal.
- %o stands for octal.
# Taking input from the user num = int(input("Enter a value: ")) multi = num *11 # Output print("The product is %d" %multi)
Enter a value: 9 The product is 99
Note: also read about Modules and Functions in Python
Please follow me to read my latest post on programming and technology if you like my post.
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.