Today our python built-in function is int() which is useful for converting a number which is in string type in to integer. This is very much useful to club non int type which is a number(The number can be of any base like binary, nibble, oct and hex) to an integer.

Need of int() function in Python:

Some times when we are working with data, numbers are treated as strings and when we try to do some manipulation with other numbers we may get errors like "TypeError: cannot concatenate ‘str’ and ‘int’ objects". This error can be eliminated by converting a number in string type to an integer. In this post we will see on how to use int() python built-in function to do this.

Python int() function syntax:

	int(‘number’,base)

From above syntax, 'number' should be a string or bytes or byte array which represents a number in a string type. And 'base' is optional and it is useful to convert this string from it’s base to integer. We can convert from any data type like string, hex, oct, binary to integer if it is represented by string type.

Examples: Let us start with int() function with some examples and see how we can use them in Python coding.

Example1: Convert a string to it’s .

	>>> int(‘23’)

Output:

	23
>>>

Example2: Adding a string which is number to other number
My variables are

	>>STR2='23'
>>NUM2=87

Adding two numbers in which one is in string format.

	>>NUM3=int(STR2)+NUM2

Output:

	110

Example3: Converting from one form to other. Convert '0100' which can be a string, binary, nibble, oct, hex formats to integer

	>>> VAR1='0100'                                                                                             
>>> int(VAR1)                                                                                               
100                                                                                                         
>>> int(VAR1,2)                                                                                             
4                                                                                                           
>>> int(VAR1,4)                                                                                             
16                                                                                                          
>>> int(VAR1,8)                                                                                             
64                                                                                                          
>>> int(VAR1,16)                                                                                            
256                                                                                                         
>>>   

Related functions: chr(), str()

The following two tabs change content below.
Mr Surendra Anne is from Vijayawada, Andhra Pradesh, India. He is a Linux/Open source supporter who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. He works as Devops Engineer with Taggle systems, an IOT automatic water metering company, Sydney . You can contact him at surendra (@) linuxnix dot com.