Type casting is the process of converting a variable or value from one data type to another. This is often necessary to perform operations that require specific data types or to optimize data storage and manipulation.
Python Table of Contents
Common Type Casting Functions
Function | Description | Example |
int() | Turns a value into an integer | int(“123”) -> 123 |
float() | Turns a value into a float | float(“3.14”) -> 3.14 |
str() | Changes a value into a string | str(123) -> “123” |
list() | Creates a list from a value | list(“hello”) -> [‘h’, ‘e’, ‘l’, ‘l’, ‘o’] |
tuple() | Forms a tuple from a value | tuple([1, 2, 3]) -> (1, 2, 3) |
set() | Builds a set from a value | set([1, 2, 2, 3]) -> {1, 2, 3} |
dict() | Transforms key-value pairs into a dictionary | dict([(1, ‘k’), (2, ‘s’)]) -> {1: ‘k’, 2: ‘s’} |
Characteristics
- Explicit Conversion:Python requires explicit conversion using built-in functions to change data types.
- String to Numeric: Commonly involves converting strings to integers or floats.
- List, Tuple, and Set: Can be converted to other collection types.
- Handling Errors: Type conversion can raise errors if the value is not compatible with the target type (e.g., converting a non-numeric string to an integer).
Examples:
Converting String to Integer and Float:
num_in_str = "123"
num_to_int = int(num_in_str in str) # Converts to integer 123
num_to_float = float(num_in_str) # Converts to float 123.0
Converting Integer to String:
age = 25
age_to_str = str(age) # Converts to string "25
Converting List to Tuple and Set:
my_list = [1, 2, 3]
list_to_tuple = tuple(my_list) # Converts to tuple (1, 2, 3)
list_to_set = set(my_list) # Converts to set {1, 2, 3}
Converting String to List:
text = "hello"
char_to_list = list(text) # Converts to list ['h', 'e', 'l', 'l', 'o']
Converting List of Tuples to Dictionary:
pairs = [(1, 'a'), (2, 'b')]
my_dict = dict(pairs) # Converts to dictionary {1: 'a', 2: 'b'}
Conclusion
Type casting is a core or fundamental concept in Python that enables the conversion of values between different data types. Mastering type casting allows for better data management and ensures smoother operations, especially when working with functions or calculations that require particular data types.
Knowledge Check

Check out our Trending Courses Demo Playlist
Data Analytics with Power Bi and Fabric |
Could Data Engineer |
Data Analytics With Power Bi Fabic |
AWS Data Engineering with Snowflake |
Azure Data Engineering |
Azure & Fabric for Power bi |
Full Stack Power Bi |
Kick Start Your Career With Our Data Job
Social Media channels
► KSR Datavizon Website :- https://www.datavizon.com
► KSR Datavizon LinkedIn :- https://www.linkedin.com/company/datavizon/
► KSR Datavizon You tube :- https://www.youtube.com/c/KSRDatavizon
► KSR Datavizon Twitter :- https://twitter.com/ksrdatavizon
► KSR Datavizon Instagram :- https://www.instagram.com/ksr_datavision
► KSR Datavizon Face book :- https://www.facebook.com/KSRConsultingServices
► KSR Datavizon Playstore :-
Most Commented