Python - Things to know before proceed - Input, Output Function and Formatting Tutorial
Input function
The input function is used to take user input at any point of time in the program using input().
#Taking input from user
name = input("Enter your name: ")
#Printing the input
print(name)
print(type(name))
Output:-
Enter your name: 123
<class 'str'>
The input function always returns a string. We can convert the return value of an input to any other data type explicitly.
To convert input to integer or float, we need to use Explicit Type Casting i.e int(), float() ,etc
#Taking input from user using int() as an explicit typecasting
name = int(input("Enter your name: "))
print(name)
#On taking input as 123, then it will provide datatype as int, instead of string
print(type(name))
Output:-
Enter your name: 123
<class 'int'>
To Take input without type Casting, we can use eval() instead of int(), float(), etc
Eval function will automatically detect data type of a user input
#Taking input from user using eval()
name = eval(input("Enter your name: "))
print(name)
#On taking input as 123.32, then it will provide datatype as float, instead of string
print(type(name))
Output:-
Enter your name: 123.32
<class 'float'>
Output function
The output function is used to display output on a console using print().
Syntax for print():-
print(value(s), sep= ā ā, end = ā\nā)
#also include parameter like file and flush
Parameter:-
value (s) - can use any number of values, and will be converted to a string before printing
sep - optional parameter, It specifies how to separate multiple values, by default it is space.
end - optional parameter, It specifies what to print at end of the string, by default it is '\n'
Example1-
print('Fresherbell')
print('Fresherbell','Python','Tutorial')
print('Fresherbell','Python','Tutorial', sep="-")
print('Fresherbell','Python','Tutorial', sep="-",end='++')
Output-
Fresherbell
Fresherbell Python Tutorial
Fresherbell-Python-Tutorial
Fresherbell-Python-Tutorial**
Example2-
a=21
b=22
c=23
d=35
e=50
print(c,e,a,end="++")
print(b,d,c,sep="\n",end='&&')
print(e,a,sep="--",end="--")
print(b,d,d,sep="==",end="fresherbell")
Output-
23 50 21++22
35
23&&50--21--22==35==35fresherbell
Output Formatting
Using String Literals
We can format the output using string literals, by starting a string with f or F before opening the quotation.
Inside quotation, we can write refer variable inside { }.
a="Python"
b="Fresherbell"
print(f'Hello {a}')
print(F'Hello {b}')
Output;-
Hello Python
Hello Fresherbell
Using format function
Formatting output can also be done using the format function, here { } curly braces in an expression are used as a placeholder, and .format() at last will hold the variable in the same order of curly braces by default.
We can also modify the order of a variable in an expression by using indexing starting from 0.
a="Python"
b="Fresherbell"
# By Default Order
print('Hello {}, Hello {}'.format(a,b))
# We can also change the order using indexing
print('Hello {1}, Hello {0}'.format(a,b))
Output:-
Hello Python, Hello Fresherbell
Hello Fresherbell, Hello Python
Using % Operator
In this % Operator with datatype symbol(i.e %d, %f, %s, etc) is used in an expression, and its associated variable is declared at last with % operator in the same order.
More than one argument has to be used inside the tuple. A single argument can be used directly.
a="Python"
b="Fresherbell"
c=3.15
# Single argument
print("c = %f" %c)
# More than one argument has to be used in tuple
print("Hello %s, Hello %s" %(a,b))
Output:-
c = 3.150000
Hello Python, Hello Fresherbell