Python Input and output functions | Learn python programming for beginners Free
Input Functions
input() function in Python enables a user of a program supply inputs to the program at run time.
input([prompt])
- [prompt] is an optional string that is getting displayed on the output screen. It is a prescription to the user to enter the input expected by the program.input() function is designed to accept the characters entered by the user through keyboard in the form of string. To enable the program to use the numbers from the string entered by the user, the string should be type converted to required type.type() function returns the data type of the argument supplied to it.
1 2 | a=input("Enter a number:") type(a) |
Enter a number:10 <class 'str'>
To convert the string input into a number they can be supplied as argument to int() or float() functions.
1 2 3 | a=int(input("Enter a number:")) print("a=",a) type(a) |
Enter a number:10 a= 10 <class 'int'>
int() and float() functions are capable of handling values. These functions are not able to convert the result of an string expression to int or float.
1 2 3 | s='2+3' sum=float(s) print("Sum of given expression is:",sum) |
Traceback (most recent call last): File "C:/Users/Soni's-PC/AppData/Local/Programs/Python/Python38/inputoutput.py", line 2, in <module> sum=float(s) ValueError: could not convert string to float: '2+3'
eval() function serves the purpose of converting a string expression into int or float.
1 2 3 4 5 | s='2+3' sum=eval(s) print("Sum of given expression is:",sum) print(type(s)) print(type(sum)) |
Sum of given expression is: 5 <class 'str'> <class 'int'>
Output Functions
Python has a standard built-in library function print() displays the strings on the Standard Output Device (Screen).
print([String])
- string is a collection of characters enclosed within single or double quotes. The strings and values of the variables are separated by a comma.
1 | print("Hi everyone") |
Hi everyone
1 2 | a=10 print("Value of a:",a) |
Value of a: 10
The print() has following syntax:
print(*objects, sep=' ', end=' ', file=sys.stdout, flush=False)
- obj is the value(s) to be printed.
- sep is the separator used between the values. The default separator is white space.
- After all the values are printed, end is printed. The default value of end is new line(” “)
- file is the object where the values are printed and its default value is sys.stdout (screen).
1 2 3 | print(1,2,3,4,5) print(1,2,3,4,5,sep='*') print(1,2,3,4,5,sep='&',end='$') |
1 2 3 4 5 1*2*3*4*5 1&2&3&4&5$
format() function helps to format the output printed on the screen with good look and attractive.
1 2 3 4 | a=10 b=15 sum=a+b print("Sum of {} and {} is {}:".format(a,b,sum)) |
Sum of 10 and 15 is: 25
Curly braces {} are just the placeholders for the values to be printed on the output screen. The order of printing values can be specified using numbers.
1 2 3 4 5 | x="apple" y="orange" print("I like {} and {}".format(x,y)) print("I like {0} and {1}".format(x,y)) print("I like {1} and {0}".format(x,y)) |
I like apple and orange I like apple and orange I like orange and apple
You must log in to post a comment.