0
0
Data-analysis-pythonHow-ToBeginner ยท 2 min read

Python How to Convert Data Types Easily

In Python, you can convert data types using built-in functions like int() to convert to integer, float() for floating-point numbers, str() for strings, and bool() for boolean values.
๐Ÿ“‹

Examples

Input"123"
Output123
Input123
Output123.0
Input0
OutputFalse
๐Ÿง 

How to Think About It

To convert data types in Python, think about what type you want to get and use the matching function like int() for whole numbers or str() for text. These functions take your original value and change it to the new type if possible.
๐Ÿ“

Algorithm

1
Identify the original data type of the value.
2
Choose the target data type you want to convert to.
3
Use the corresponding Python function like int(), float(), str(), or bool() to convert the value.
4
Handle any errors if the conversion is not possible.
๐Ÿ’ป

Code

python
value_str = "123"
value_int = int(value_str)
value_float = float(value_int)
value_bool = bool(value_int)
value_str_again = str(value_bool)

print(value_int)
print(value_float)
print(value_bool)
print(value_str_again)
Output
123 123.0 True True
๐Ÿ”

Dry Run

Let's trace converting the string "123" to int, then float, then bool, then back to string.

1

Convert string to int

value_str = "123" -> int(value_str) = 123

2

Convert int to float

value_int = 123 -> float(value_int) = 123.0

3

Convert int to bool

value_int = 123 -> bool(value_int) = True

StepValueType
Initial"123"str
After int()123int
After float()123.0float
After bool()Truebool
๐Ÿ’ก

Why This Works

Step 1: Using int() to convert to integer

The int() function takes a string or number and converts it to an integer if possible.

Step 2: Using float() to convert to decimal number

The float() function converts integers or strings to floating-point numbers.

Step 3: Using bool() to convert to boolean

The bool() function converts values to True or False, where zero or empty values become False.

Step 4: Using str() to convert to string

The str() function converts any value to its string representation.

๐Ÿ”„

Alternative Approaches

Using eval() for dynamic type conversion
python
value = "123"
converted = eval(value)  # Converts string '123' to int 123
print(converted)
eval() can convert strings to their Python types but is unsafe with untrusted input.
Using ast.literal_eval() for safe evaluation
python
import ast
value = "123"
converted = ast.literal_eval(value)  # Safely converts string to int
print(converted)
Safer than eval(), but only works with literals like numbers, strings, tuples, lists.
โšก

Complexity: O(1) time, O(1) space

Time Complexity

Conversion functions like int(), float(), str(), and bool() run in constant time because they process a single value without loops.

Space Complexity

These functions use constant extra space since they create a new value of the target type without additional data structures.

Which Approach is Fastest?

Built-in functions like int() and float() are fastest and safest; eval() is slower and risky, while ast.literal_eval() is safer but slightly slower.

ApproachTimeSpaceBest For
int()/float()/str()/bool()O(1)O(1)Simple, safe type conversion
eval()O(1)O(1)Dynamic but unsafe conversion
ast.literal_eval()O(1)O(1)Safe evaluation of literals
๐Ÿ’ก
Always use the specific conversion function like int() or float() instead of eval() for safety and clarity.
โš ๏ธ
Trying to convert a string that doesn't represent a number using int() causes a ValueError.