How To Convert String To Int In Python?

Use `int(“123”)` to convert a numeric string to an integer

Use `int(float(“123.45”))` if the string contains a decimal number and you want to truncate it

Use `int(“1010”, 2)` to convert a binary string to an integer

Use `int(“FF”, 16)` to convert a hexadecimal string to an integer

Use `int(” 42 “)` to convert a string with surrounding whitespace

Use `try` and `except ValueError` to handle invalid strings safely

Use `int(my_string)` when `my_string` already contains only digits or a valid signed integer string

Suggested for You

Trending Today