Casting means changing a data type to another. It is popular in many programming languages like C/C++, Java, etc. Python also supports the type casting and it is done using casting functions.
In python, there are two primary casting operations you can do and they are:
- Change string to other types such as Integer, Float, Boolean types.
- Convert other types to string
Here are a list functions involved in casting from one type to another.
Examples of Type Casting in Python
Integer to String
num = 2233 result = str(num) print(type(result))
The output of the above code is
==== RESTART: C:/Python_projects/type casting/integer_to_string.py ======= <class 'str'> >>>
Float to String
num_float = 344.55 result = str(num_float) print(type(result))
Once again the output is a string. Note that we are only checking the type and not printing the actual value.
===== RESTART: C:/Python_projects/type casting/float_to_string.py ======== <class 'str'> >>>
Boolean to String
bool = True result = str(bool) print(type(result))
Here is the output of the above code. Once again it is string, the value
True will be
"True". Note the double quotes around.
===== RESTART: C:/Python_projects/type casting/Bool_to_String.py ========= <class 'str'> >>>
Examples of String Type to Other Types
In this section, we will convert other types to its string equivalent.
String to Integer
str = "Hello World" str2 = "45" result = int(str) print(type(result))
The output of the above program is given below. The
str cannot be converted into an integer and produce following error.
==== RESTART: C:/Python_projects/type casting/String to Integer.py ======= Traceback (most recent call last): File "C:/Python_projects/type casting/String to Integer.py", line 3, in <module> result = int(str) ValueError: invalid literal for int() with base 10: 'Hello World' >>>
However, the string “45” is converted to number 45 successfully. See the output below.
==== RESTART: C:/Python_projects/type casting/String to Integer.py ======= <class 'int'> >>>
String to Float
Now we will check two cases , first we will
- convert a number string to float
- convert a float number string to float
numString = "56" result = float(numString) print(type(result))
This was successful and the output is below.
======= RESTART: C:/Python_projects/Number_String_To_Float.py =========== <class 'float'> >>>
Now we will convert a float string to float value.
float_string = "3.14" result = float(float_string) print(result) print(type(result))
The output is given below.
======= RESTART: C:/Python_projects/Float_String_to_Float.py ========= 3.14 <class 'float'> >>>
String to Boolean Value
To convert a boolean string such as “True” or “False”, you have to use the
ast or Abstract Syntax Trees.
Visit following link to learn more about – ast
It has many utility function and one of them is
ast.literal_aval() that evaluates string to other types such as Boolean.
import ast boolString = "True" result = ast.literal_eval(boolString) print(result) print(type(result))
The output is given below. Note that the result and its type both are printed.
======= RESTART: C:/Python_projects/bool_string_to_boolean.py ======== True <class 'bool'> >>>