python / beginner
Snippet
Type Conversion
Type conversion (casting) is used to change a value from one data type to another, such as converting a string to an integer.
snippet.py
1
2
3
4
age_str = "25"age_int = int(age_str)result = age_int + 5print(result)
Breakdown
1
age_str = "25"
Stores a numeric value as a string.
2
age_int = int(age_str)
Converts the string to an actual integer so math can be performed.
3
result = age_int + 5
Adds 5 to the integer value.