String:
String is an immutable sequence data type in Python. It’s a string of Unicode characters enclosed by single, double, or triple quotes.
Given a string , the task is to convert the given string to integer.
Convert String to Integer in Python
- int() function
- Converting given string in integer
- Converting Binary string to integer
- Converting Hexadecimal string to integer
- Converting octal string to integer
- Converting a number string with a comma to an integer
1)int() function
The int() function returns an integer number from the specified value.
If no arguments are provided, the int() function returns an integer object constructed from a number or string x, or it returns 0.
Syntax:
int(string, base)
Parameters:
string : consists of 1's and 0's ( or ) The string that contains a number, such as ‘9347' or ‘1734' and so on. base : (integer value) base of the number. 10 is the default value of base.
Returns :
Returns an integer value in the given base that is the binary string's equivalent.
2)Converting given string in integer
Assume we have a string as a Str object that contains the value ‘28119′. We’ll pass the string to the int() function to convert it to an integer, or int object. Which returns the int object after converting this string to an integer.
Below is the implementation:
# given string value string_value = '28119' # converting given string to integer integer = int(string_value) # printing the integer value print(integer) # printing the type after converting string to integer print('printing the type of object after converting string to integer', type(integer))
Output:
28119 printing the type of object after converting string to integer <class 'int'>
Explanation:
Because we didn't provide a base argument, the base value 10 was used by default when converting the string to an integer.
3)Converting Binary string to integer
Assume we have a Str object with the string ‘10101′. It contains a number’s binary representation. We’ll pass this string to the int() function along with the base value 2 to convert it to an integer, i.e., an int object. The int() function then converts the binary string to an integer and returns a value of int.
Below is the implementation:
# given binary string value binary_String = '10101' # converting given binarystring to integer integer = int(binary_String, 2) # printing the integer value print(integer)
Output:
21
4)Converting Hexadecimal string to integer
Assume we have a Str object with the string ‘0x6DCF’. It contains a number’s hexadecimal representation. We’ll pass this string to the int() function along with the base value 16 to convert it to an integer, i.e., an int object. The int() function then converts the hex string to an integer and returns the object int.
Below is the implementation:
# given hexadecimal string value hexadecimal_string = '0x6DCF' # converting given hexadecimal to integer integer = int(hexadecimal_string, 16) # printing the integer value print(integer)
Output:
28111
ValueError will be raised if the base value is not set to 16 with a hex string in the int() function. When converting a hexadecimal string to an int object, always remember to set the base value to 16.
5)Converting octal string to integer
Assume we have a Str object with the string ‘0o103650′. It contains a number’s octadecimal representation. We’ll pass this string to the int() function along with the base value 8 to convert it to an integer, i.e., an int object. The int() function then converts the octadecimal to an integer and returns the object int.
Below is the implementation:
# given octal string value octal_string = '0o103650' # converting given octal_string to integer integer = int(octal_string, 8) # printing the integer value print(integer)
Output:
34728
6)Converting a number string with a comma to an integer
We can convert the number string with comma to integer . By removing all the ‘,’ from the string by replacing the ‘,’ with empty character and then we can use int() to convert it to integer.
Below is the implementation:
# given string given_string = '2803,11,02' # replacing the , with empty charcater using replace() function given_string = given_string.replace(',', "") # converting the string to integer intvalue = int(given_string) # print the string print(intvalue)
Output:
28031102
Related Programs: