cast to int python code example
Example 1: check if string can be converted to int python
variable='3'
try:
int(variable)
print('You typed an int')
except ValueError:
print('You did not type an int')
variable='3'
try:
float(variable)
print('You typed a float')
except ValueError:
print('You did not type a float')
Example 2: python string to int
string = '123'
integer = int(string)
integer
Example 3: convert float to int python
x=3.1415
y=int(x)
print(y)
Example 4: python cast to float
float('1.234')
Example 5: python string to int
print(int("12"))
Example 6: python cast to int
x = int(1)
y = int(2.8)
z = int("3")