How to change variable datatype in Python?

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.

Sample Output

String to Integer datatype conversion in Python
String to Integer datatype conversion in Python


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.

Sample Output

Integer to String datatype conversion in Python
Integer to String datatype conversion in Python


integer to float

This type of datatype conversion is used when we need decimal point precision in the numbers.

Sample Output

Integer to Float datatype conversion in Python
Integer to Float datatype conversion in Python


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.

Sample Output

Float to Integer datatype conversion in Python
Float to Integer datatype conversion in Python


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.

Sample Output

String to Date datatype conversion in python
String to Date datatype conversion in python


Popular String to date formats

Sample Output

Popular string to date formats

Author Details
Lead Data Scientist
Farukh is an innovator in solving industry problems using Artificial intelligence. His expertise is backed with 10 years of industry experience. Being a senior data scientist he is responsible for designing the AI/ML solution to provide maximum gains for the clients. As a thought leader, his focus is on solving the key business problems of the CPG Industry. He has worked across different domains like Telecom, Insurance, and Logistics. He has worked with global tech leaders including Infosys, IBM, and Persistent systems. His passion to teach inspired him to create this website!

Leave a Reply!

Your email address will not be published. Required fields are marked *