Type Conversion in Python

Type Conversion in Python

Type Conversion

typecast in python, type conversion in python, data type conversion in python


With the help of python we can convert data into different types, there are different functions are available for this operation. This is very useful in competitive programming.

1. int(a,base) : This function converts any data type to an integer. ‘Base’ specifies the base in which string is if data type is string.

2. float() : This function is used to convert any data type to a floating point number.

# Python code to show Type conversion 
# using int(), float() 
  
# initializing string 
s = "100010"
  
# printing string converting to int base 2 
c = int(s,2) 
print ("After converting to an integer of base 2 : ", end="") 
print (c) 
  
# printing string converting to float 
e = float(s) 
print ("After converting to an float : ", end="") 
print (e) 

Output

After converting to an integer of base 2 : 34
After converting to an float : 100010.0

Lets see an another example 

str_number = '78'

print(int(str_number))        #default base is 10

print(int(str_number, 16))    #From Hexadecimal

print(int(str_number, 12))    #From a number where base is 12

str_number = '25.897'

print(float(str_number))      #convert string to floating point value

Output 

78
120
92
25.897

3. ord() : This function is used to convert a character to integer.

4. hex() : This function is to convert integer to hexadecimal string.

5. oct() : This function is to convert integer to octal string.

6. bin() : This function is to convert integer to binary number.

# Python code to show Type conversion


# using ord(), hex(), oct()


# initializing integer


s = '8'



# printing character converting to integer


c = ord(s)


print ("after converting character to integer : ",end="")


print (c)


# printing integer converting to hexadecimal string


c = hex(56)


print ("after converting 56 to hexadecimal string : ",end="")


print (c)


# printing integer converting to octal string


c = oct(56)


print (" after converting 56 to octal string : ",end="")


print (c) 


Output 

after converting character to integer : 56
after converting 56 to hexadecimal string : 0x38
 after converting 56 to octal string : 0o70


Lets see an another example related to this 

# Python code to show Type conversion 

# using  ord(), hex(), oct(), bin()

print('ASCII value of "G" is: ' + str(ord('G')))


print('Hexadecimal value of 254 is: ' + str(hex(256)))


print('Octal value of 62 is: ' + str(oct(54)))


print('Binary value of 56 is: ' + str(bin(68)))



Output 

ASCII value of "G" is: 71
Hexadecimal value of 254 is: 0x100
Octal value of 62 is: 0o66
Binary value of 56 is: 0b1000100


7. tuple() : This function is used to convert to a tuple.

8. set() : This function returns the type after converting to set.

9. list() : This function is used to convert any data type to a list type.

# initializing string 

s = 'code'

# printing string converting to tuple 

c = tuple(s) 

print ("After converting string to tuple : ",end="") 

print (c)  

# printing string converting to set 

c = set(s) 

print ("After converting string to set : ",end="") 

print (c) 

 # printing string converting to list 

c = list(s) 

print ("After converting string to list : ",end="") 

print (c) 


Output 

After converting string to tuple : ('c', 'o', 'd', 'e')
After converting string to set : {'d', 'e', 'o', 'c'}
After converting string to list : ['c', 'o', 'd', 'e']


Lets have a look on a one more example


cod_list = [10, 25, 35, 46, 59, 64]


cod_set = {10, 11, 22, 33, 55, 28, 26, 79}


print('From list to tuple: ' + str(tuple(cod_list)))


print('From list to set: ' + str(set(cod_list)))

print('From set to list: ' + str(list(cod_set)))



Output 

From list to tuple: (10, 25, 35, 46, 59, 64)
From list to set: {64, 35, 10, 46, 25, 59}
From set to list: [33, 10, 11, 79, 22, 55, 26, 28]


11. str() : Used to convert integer into a string.

12. complex(real,img) : : This function converts real numbers to complex(real,img) number. we can convert two integers (real and imaginary part) to complex numbers.

A simple example to demonstrate this.

#convert to complex number


my_complex = complex(10, 5)

print(my_complex)



Output 

(10+5j)

One more example to understand 

# using  dict(), complex(), str() 
  
# initializing integers 
a = 1
b = 2
  
# initializing tuple 
tup = (('c', 1) ,('o', 2), ('d', 3)) 
  
# printing integer converting to complex number 
c = complex(1,2) 
print ("After converting integer to complex number : ",end="") 
print (c) 
  
# printing integer converting to string 
c = str(a) 
print ("After converting integer to string : ",end="") 
print (c) 
  
# printing tuple converting to expression dictionary 
c = dict(tup) 
print ("After converting tuple to dictionary : ",end="") 
print (c)

Output 

After converting integer to complex number : (1+2j)
After converting integer to string : 1
After converting tuple to dictionary : {'c': 1, 'o': 2, 'd': 3}

13. chr(number) : : This function converts number to its corresponding ASCII character.

# Convert ASCII value to characters
a = chr(78)
b = chr(75)
  
print(a)
print(b)

Output:

N
K


in this article i aimed to explain the type conversion in python


Recommended Post : 







#python global
#python global variable#python programming
#python list
#learn python
#python requests
#introduction to data science in python
#free python tutorial
#learn python basics
#python study
#python and ai













Post a Comment

1 Comments

If you have any doubt, Please let me know.