Python Type Conversion
In Python, type conversion refers to the process of converting one data type to another. Python provides built-in functions for type conversion, allowing you to convert integers, floats, strings, and other types between one other.
Basic Type Conversion in Python
Python has several built-in functions that allow for type conversion, such as int(), float(), str(), and others. Here's how these work:
1. Converting from String to Integer
You can convert a string containing a number into an integer using the int() function:
string_value = "42"
integer_value = int(string_value)
print(integer_value) # Output: 42
string_value = "42"
integer_value = int(string_value)
print(integer_value) # Output: 42
2. Converting from String to Float
Similarly, you can convert a string to a float using the float() function:
string_value = "42.5"
float_value = float(string_value)
print(float_value) # Output: 42.5
string_value = "42.5"
float_value = float(string_value)
print(float_value) # Output: 42.5
3. Converting from Integer to String
You can convert an integer into a string using the str() function:
integer_value = 42
string_value = str(integer_value)
print(string_value) # Output: '42'
integer_value = 42
string_value = str(integer_value)
print(string_value) # Output: '42'
4. Converting from Float to Integer
To convert a float to an integer, you can use int(), but it will truncate the decimal part (i.e., it does not round):
float_value = 42.8
integer_value = int(float_value)
print(integer_value) # Output: 42
float_value = 42.8
integer_value = int(float_value)
print(integer_value) # Output: 42
Explicit Type Conversion
Explicit type conversion is when you manually convert a variable from one type to another using functions like int(), float(), and str().
Explicit Conversion Example: String to Integer
string_value = "123"
integer_value = int(string_value) # Convert string to int
print(integer_value) # Output: 123
string_value = "123"
integer_value = int(string_value) # Convert string to int
print(integer_value) # Output: 123
Explicit Conversion Example: Integer to String
integer_value = 123
string_value = str(integer_value) # Convert int to string
print(string_value) # Output: '123'
integer_value = 123
string_value = str(integer_value) # Convert int to string
print(string_value) # Output: '123'
Implicit Type Conversion
Implicit type conversion, also known as "type coercion," occurs automatically when Python converts one data type to another. This typically happens when you perform an operation that involves two different types of data.
Implicit Conversion Example: Integer to Float
integer_value = 5
float_value = 2.5
result = integer_value + float_value # Implicit conversion to float
print(result) # Output: 7.5
integer_value = 5
float_value = 2.5
result = integer_value + float_value # Implicit conversion to float
print(result) # Output: 7.5
Implicit Conversion Example: Concatenating String and Integer
integer_value = 42
string_value = " apples"
result = str(integer_value) + string_value # Implicit conversion of integer to string
print(result) # Output: '42 apples'
integer_value = 42
string_value = " apples"
result = str(integer_value) + string_value # Implicit conversion of integer to string
print(result) # Output: '42 apples'
Frequently Asked Questions
What is type conversion in Python?
What is type conversion in Python?
Type conversion means changing the data type of a value, like turning a string into an integer using int().
What is the difference between implicit and explicit type conversion?
What is the difference between implicit and explicit type conversion?
Implicit conversion is automatic (e.g., adding int and float), while explicit conversion uses functions like float(), int(), or str().
How do I convert a string to an integer in Python?
How do I convert a string to an integer in Python?
Use int(). Example: int("42") converts the string "42" into the integer 42.
What happens if I convert a float to an integer?
What happens if I convert a float to an integer?
Python will truncate the decimal, not round it. For instance, int(4.9) results in 4.
Can I convert a list to a tuple or vice versa?
Can I convert a list to a tuple or vice versa?
Yes! Use tuple() to convert a list to a tuple, and list() to convert a tuple to a list.
What's Next?
Next, you'll learn about Python comments, which are essential for documenting and explaining your code. You'll discover how to use single-line and multi-line comments, as well as the importance of keeping your code readable and maintainable.