How To Convert String To Integer In Python?

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

Use `int(float(“123.45”))` to convert a decimal string to an integer by truncating the decimal part

Use `int(“123”, 10)` to convert a base-10 string explicitly

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(s.strip())` to remove surrounding whitespace before conversion

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

Suggested for You

Trending Today