Skip to Main Content

Introduction to Python

This guide was designed to supplement the INTRODUCTION TO PYTHON virtual workshop. If you have concerns or suggestions for this guide, feel free to reach out to scholarlyengagement@tulane.edu.

Checking and Changing Data Type

As you learned in the Data Types & Variable Assignment section, there are multiple data types to meet your data needs in Python. If your data needs change or if your data was imported incorrectly, you can change data types.

In this section, you will learn how to:

  • Convert data types 

Copy the following code on the left in your IDE to execute the process shown in the image on the right.


Checking Data Types | type(variable)                         

ic = "34"                                                                                      
print(type(ic))

 

 


Converting to String | str(variable)

ic = 34                                                                                     
print(ic)                                                                      

ic = str(ic)
print(type(ic))

 

 


Converting to Integer | int(variable)

ic = "34"                                                                           
print(ic)
ic = int(ic)
print(type(ic))

 

 

 


Converting to Float | float(variable)

ic = "34"                                                                              
print(ic)
ic = float(ic)
print(type(ic))

 

 

 


Converting from String to List | list.split("separator")

lis = "Tulane University Libraries"                                           

lis= lis.split(" ")
print(type(lis))

 

lis2 = "she, has, a, new, major"
lis2 = lis2.split(",")

print(type(lis2))

 

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.