How to Convert String to an Int in Python

Converting string to int in Python is easy using the built-in int() function:

num_str = "100"

num_int = int(num_str)

To convert a comma-separated number string (such as 1,000,000) to an int in Python, first, get rid of the commas with the replace() method. Then convert the result to an int using the int() function:

big_num_str = "80,129,553"

big_num_int = int(big_num_str.replace(",", ""))
print(big_num_int)

Output:

80129553

For the sake of completeness, to convert an int to a string, use the built-in str() function:

num_int = 100

num_str = str(100)

To convert an integer to a comma-separated number string:

big_num = 92444232493

big_num_cs = "{:,}".format(big_num)

print(big_num_cs)

Output:

92,444,232,493

String to Int Conversion in Python

Converting string to int in Python happens using the built-in int() function.

int("25")

Using int() to convert a string to an integer assumes that a string represents a decimal integer.

For example, if you pass a hexadecimal string into the int() function, you see an error.

For example:

int("0x6F")

Outcome:

ValueError: invalid literal for int() with base 10: '0x6F'

This ValueError says that the string you tried to convert is not a valid decimal integer.

There is an easy way to fix this problem.

When you use the int() function to convert a string to an int, you can specify the base to it. The base is the integer system you are using.

To convert a hexadecimal string to an int, let the int() function know you’re dealing with a 16-base system:

int("0x6F", base=16) # '111'

Also, notice that you are not restricted to use base 16 only. You can specify any valid number base as the base in the int() function call.

Int to String Conversion in Python

As you already saw, you can convert an integer to a string using the built-in str() function.

num_int = 100

str(100) # '100'

This is the basic use case. But if you are dealing with other numeric systems, read along.

The str() function is clever enough to tell that a binary literal is passed into it. For example, let’s convert a binary integer 0011 to a string that represents a regular integer:

bin = 0b0011

str(bin) # '3'

But if you want to represent an integer as a string in another number system, use the f-string formatting.

The number systems into which you can convert an integer are binary, hexadecimal, and octal.

Examples:

num = 100

num_binary_str      = f"{num:b}" # '1100100'
num_hexadecimal_str = f"{num:x}" # '64'
num_octal_str       = f"{num:o}" # '144'

Conclusion

Converting string to int in Python is easy with the built-in int() function:

int("300") # 300

To convert an integer to a string, use the built-in str() function.

str(23) # '23'

In addition, you learned how to specify numeric systems between the conversions.

num = 100

num_binary_str      = f"{num:b}" # '1100100'
num_hexadecimal_str = f"{num:x}" # '64'
num_octal_str       = f"{num:o}" # '144'

Thanks for reading. I hope you find it useful.

Happy coding!

Further Reading

50 Python Interview Questions and Answers

50+ Buzzwords of Web Development

Share on facebook
Facebook
Share on google
Google+
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on pinterest
Pinterest

Leave a Comment

Your email address will not be published. Required fields are marked *