Skip to main content

How to Convert String to int in Python

Published 1 min readMarkdown
How to Convert String to int in Python

Python has a built-in function int() which converts the passed argument into integer format.

Syntax

int(object, encoding=encoding, errors=errors)

Code

The following code illustrates the usage of the int function in Python.

# An string version of a number
string_num = 2021

# An integer version of year
integer_num = int(string_num)

print "The number is", integer_num

# Returns 0 if no argument is provided
print(int())

No comments yet. Be the first to comment.

More from this blog