[vc_row css_animation=”” row_type=”row” use_row_as_full_screen_section=”no” type=”full_width” angled_section=”no” text_align=”left” background_image_as_pattern=”without_pattern”][vc_column][vc_column_text]

Converting Data Types in Python

[/vc_column_text][vc_separator type=”normal” up=”1″][vc_column_text css=”.vc_custom_1570719264948{background-color: #f6f6f6 !important;}”]

Type conversion is used to converting one data type to another. For example, it is common to convert a string to an int or vice versa. This can be done with Python’s built-in functions such as int (), str () or float ()

[/vc_column_text][vc_empty_space height=”20px”][vc_column_text]Python and many other code languages are so-called typed languages. This means that we need to keep track of what data type variables have as we use them for different operations. If the problem that an operation cannot be performed due to the data types does not match, we can simply solve the problem by using built-in methods in Python.

 

Let’s see a short example on why it is important to keep track of the data type a variable contains

 

a = "1"
b = 2
print(a+b)

Will lead to an Error and cause the program to crash, because you cannot add an int to a string (add an integer to a text string).[/vc_column_text][vc_empty_space height=”20px”][vc_column_text]

Type conversion is used to convert one data type to a different one

[/vc_column_text][vc_empty_space height=”20px”][vc_column_text]This problem is solved by casting (converting) one data type to another. Python has built-in features that allow you to directly convert one data type to another.[/vc_column_text][vc_empty_space height=”10px”][vc_column_text]

[/vc_column_text][vc_empty_space height=”10px”][vc_separator type=”normal” up=”1″][vc_empty_space height=”10px”][vc_column_text]

CONVERT DATA TYPE IN PYTHON – EXAMPLE

Let’s take the same variables as we used in the previous example, and show how to convert the variable a (of the data type string), to the data type int

 

a = "1"
b = 2 
print(int(a)+b)

Note the highlighted code, Using the function int (), the variable a is converted from a string to an int, which makes the operation feasible!

 

Additionally, the corresponding operations can be done with:

  • str () – converts a variable to a string
  • float () – converts a variable to a float

[/vc_column_text][vc_empty_space height=”20px”][vc_column_text]

[/vc_column_text][vc_empty_space height=”10px”][vc_separator type=”normal” up=”1″][vc_empty_space height=”10px”][vc_column_text]

Keep in mind on what you are trying to convert

Note that all conversions will not succeed. For example, the following code will not work:

 

a = "2.3"
int(a)

will cause an error since 2.3 is not an int (integer) but a float.

 

However, the following code works fine

 

a = "2.3"
float(a)

[/vc_column_text][vc_empty_space height=”20px”][vc_separator type=”normal”][vc_empty_space height=”12px”][/vc_column][/vc_row][vc_row css_animation=”” row_type=”row” use_row_as_full_screen_section=”no” type=”full_width” angled_section=”no” text_align=”left” background_image_as_pattern=”without_pattern”][vc_column][vc_column_text]

Previous Page    |    Next Page

[/vc_column_text][vc_empty_space][/vc_column][/vc_row]