HowTo: Python Convert a String Into Integer

To convert a string into an integer in Python, you can use the int() function. Here’s an example:

string = "123"
integer = int(string)
print(integer)

This will output:

123

You can also specify the base of the number if the string represents a number in a different base. For example, to convert a hexadecimal string to an integer, you can do the following:

string = "0xFF"
integer = int(string, 16)
print(integer)

This will output:

255

It’s that simple! Just pass the string you want to convert to the int() function, and you’ll get back the corresponding integer value.

Leave a Comment