How To Convert Data Types in Python 3
How To Convert Data Types in Python 3
Python is a dynamically typed language, meaning that data types are inferred at runtime. However, sometimes it's necessary to convert data from one type to another. Here's how to do it:
Converting to Integer
To convert a variable to an integer, use the int() function:
x = 5.5
y = int(x)
print(y)
This will output 5.
Converting to Float
To convert a variable to a float, use the float() function:
x = "3.14"
y = float(x)
print(y)
This will output 3.14.
Converting to String
To convert a variable to a string, use the str() function:
x = 123
y = str(x)
print(y)
This will output "123".
Комментарии
Отправить комментарий