There are lots of datatype conversion which are possible in python. I am listing some of the commonly used conversions which are important.
- string to integer
- integer to string
- integer to float
- float to integer
- string to date
string to integer
This type of conversion is used when need to perform mathematical operations on a part of string which is number.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
'''string to integer datatype conversion''' # defining int as an string intVar='10' # Print datatype before conversion print('Datatype Before conversion',type(intVar)) # Converting the datatype to integer intVar=int(intVar) # Print datatype after conversion print('Datatype after conversion',type(intVar)) # common use in performing mathematical opeations # which is not possible on strings print('The percentage of products sold were',100*(intVar/100),'%') |
Sample Output

integer to string
This type of conversion is commonly used when we need to combine a string and number together into a single string message.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
'''integer to string datatype conversion''' # defining an integer variable intVar=10 # Print datatype before conversion print('Datatype Before converion',type(intVar)) # Converting the datatype to integer intVar=str(intVar) # Print datatype after conversion print('Datatype after conversion',type(intVar)) # common use in creating custom messages strMessage='The number of products sold are ' + str(200) + ' units' print(strMessage) |
Sample Output

integer to float
This type of datatype conversion is used when we need decimal point precision in the numbers.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# integer to float datatype conversion # defining an integer variable intVar=10 # Print datatype before conversion print('Datatype Before conversion',type(intVar)) # Converting the datatype to integer intVar=float(intVar) # Print datatype after conversion print('Datatype after conversion',type(intVar)) # common use in getting decimal precision strMessage='The percentage of products sold are ' + str(float(10)) + ' units' print(strMessage) |
Sample Output

float to integer
This type of conversion is commonly used when we are representing values which are quantities and generated as a result of some division operation.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# float to integer datatype conversion # defining an integer variable floatVar=10.22 # Print datatype before conversion print('Datatype Before conversion',type(floatVar)) # Converting the datatype to integer # and assigning to new variable intVar=int(floatVar) # Print datatype after conversion print('Datatype after conversion',type(intVar)) # common use in representing quantities strMessage='The product per person are ' + str(int(200/22)) + ' units' print(strMessage) |
Sample Output

String to Date
Date is a special datatype, although date values look like strings but, there are many operations we can perform using a date like comparison, difference in date etc. Most of time you get a date string as an input to your applications/programs. Hence, we must convert that string type to date type using library “datetime” in order to perform the date operations.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# String to Date datatype conversion # defining an string variable stringDate='21-10-2020' # Print datatype before conversion print('Datatype Before conversion',type(stringDate)) # Converting the datatype to Date # and assigning to new variable import datetime StartDate=datetime.datetime.strptime(stringDate, '%d-%m-%Y') # Print datatype after conversion print('Datatype after conversion',type(StartDate)) print(StartDate) # common use in finding difference in days from other date Deadline=datetime.datetime(2021,12,24) DateDifference=Deadline-StartDate print('Difference in the deadline and start date is:',DateDifference.days, 'days') |
Sample Output

Popular String to date formats
|
1 2 3 4 5 6 7 8 |
# importing the library import datetime # Generating dates using various string input formats print(datetime.datetime.strptime('22-April-2020', '%d-%B-%Y')) print(datetime.datetime.strptime('22-04-2020', '%d-%m-%Y')) print(datetime.datetime.strptime('04/22/2020', '%m/%d/%Y')) print(datetime.datetime.strptime('Mar/22/2020', '%b/%d/%Y')) |
Sample Output

