The print function enables a Python program to display textual information to the user.
Programs may use the input function to obtain information from the user.
The simplest use of the input function assigns a string to a variable:
x = input()
The parentheses are empty because, the input function does not require any information to do its job. Below program demonstrates that the input function produces a string value.
>>>x = input()
#enter text
'hi welcome to Ravula's Tutorials'
#prints value of variable x
>>>print(x)
hi welcome to Ravula's Tutorials
The eval function dynamically translates the text provided by the user into an executable form that the program can process
This allows users to provide input in a variety of flexible ways; for example, users can enter multiple entries separated by commas, and the eval function evaluates it as a Python tuple
>>>num1, num2 = eval(input('Please enter number 1, number 2: '))
>>>print(num1, '+', num2, '=', num1 + num2)
The following sample run shows how the user now must enter the two numbers at the same time separated by a comma:
Please enter number 1, number 2: 23, 10
23 + 10 = 33
>>>x=input()
3
>>>print(type(x))
class 'str'
>>>x=int(input())
3
>>>print(type(x))
class 'int'
int(a,base) : This function converts any data type to integer. ‘Base’ specifies the base in which string is if data type is string.
# initializing string
>>>s = "10010"
# printing string converting to int base 2
>>>c = int(s,2)
>>>print ("After converting to integer base 2 : ", end="")
>>>print (c)
After converting to integer base 2 : 18
float() : This function is used to convert any data type to a floating point number.
# initializing string
>>>s = "10010"
# printing string converting to float
>>>e = float(s)
>>>print ("After converting to float : ", end="")
>>>print (e)
After converting to float : 10010.0
ord() : This function is used to convert a character to integer.
# initializing string
>>>s = '4'
# printing character converting to integer
>>c = ord(s)
>>>print ("After converting character to integer : ",end="")
>>>print (c)
After converting character to integer : 52
hex() : This function is to convert integer to hexadecimal string.
# initializing string
>>>s = "4"
# printing integer converting to hexadecimal string
>>>c = hex(56)
>>>print ("After converting 56 to hexadecimal string : ",end="")
>>>print (c)
After converting 56 to hexadecimal string : 0x38
oct() : This function is to convert integer to octal string.
# initializing string
>>>s = "10010"
# printing integer converting to octal string
>>>c = oct(56)
>>>print ("After converting 56 to octal string : ",end="")
>>>print (c)
After converting 56 to octal string : 0o70
tuple() : This function is used to convert to a tuple.
# initializing string
>>>s = 'geeks'
# printing string converting to tuple
>>>c = tuple(s) )
>>>print ("After converting string to tuple : ",end="")
>>>print (c)
After converting string to tuple : ('g', 'e', 'e', 'k', 's')
set() : This function returns the type after converting to set.
# initializing string
>>>s = 'geeks'
# printing string converting to set
>>>c = set(s)
>>>print ("After converting string to set : ",end="")
>>>print (c)
After converting string to set : {'k', 'e', 's', 'g'}
list() : This function is used to convert any data type to a list type.
# initializing string
>>>s = 'geeks'
# printing string converting to list
>>>c = list(s)
>>>print ("After converting string to list : ",end="")
>>>print (c)
After converting string to list : ['g', 'e', 'e', 'k', 's']
dict() : This function is used to convert a tuple of order (key,value) into a dictionary.
# initializing integers
>>>a,b=1,2
# initializing tuple
>>>tup = (('a', 1) ,('f', 2), ('g', 3))
# printing integer converting to complex number
>>>c = complex(1,2)
>>>print ("After converting integer to complex number : ",end="")
>>>print (c)
After converting integer to complex number : (1+2j)
str() : Used to convert integer into a string.
# initializing integers
>>>a,b=1,2
# initializing tuple
>>>tup = (('a', 1) ,('f', 2), ('g', 3))
# printing integer converting to string
>>>c = str(a)
>>>print ("After converting integer to string : ",end="")
>>>print (c)
After converting integer to string : 1
complex(real,imag) : This function converts real numbers to complex(real,imag) number.
# initializing integers
>>>a,b=1,2
# initializing tuple
>>>tup = (('a', 1) ,('f', 2), ('g', 3))
# printing tuple converting to expression dictionary
>>>c = dict(tup)
>>>print ("After converting tuple to dictionary : ",end="")
>>>print (c)
After converting tuple to dictionary : {'a': 1, 'f': 2, 'g': 3}