12-04-2022, 08:39 AM
How to Convert Octal String to Integer in Python
5/5 – (1 vote)
![[Image: oct_to_int.gif]](https://blog.finxter.com/wp-content/uploads/2022/12/oct_to_int.gif)
Problem Formulation
Given a string in the octal form:
How to convert the octal string to an integer in Python?
For example, you want to convert the octal string to the decimal integer .
Here are a few other examples:
Octal String |
Decimal
|
0
|
4
|
8
|
12
|
16
|
63
|
32767
Oct String to Integer using int() with Base 8
To convert an octal string to an integer, pass the string as a first argument into Python’sbuilt-in function. Use as a second argument of the function to specify that the given string is an octal number. The function will then convert the octal string to an integer with base 10 and return the result.
Here’s a minimal example:
Examples
And here’s how you can convert the additional examples shown above:
You actually don’t need to use the prefix because your second argument already defines unambiguously that the given string is an octal number:
However, skipping the base but leaving the prefix raises a:
It assumes that the input string is in base 10 when in fact, it isn’t.
Note: Even though passing a prefixed string into the function is unambiguous, Python’s function doesn’t accept it if you don’t also define the base. This may be fixed in future versions!
In fact, you can specify the base argument as to switch on base guessing—which should be the default behavior anyway!![[Image: 1f447.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f447.png)
Base Guessing
You can pass a prefixed string into the function and set the base to to switch on base guessing in Python. This uses the prefix to determine the base automatically—without you needing to set it to . Yet, you still have to set it to so the benefit is marginal in practice.
Converting Octal Literals to Int
![[Image: image-40.png]](https://blog.finxter.com/wp-content/uploads/2022/12/image-40.png)
If you don’t have an octal string but a octal number—called a literal—such as, you don’t even need the function because Python will automatically convert it to a decimal number:
Background int()
Argument |
|
A Python object to be converted into an integer number. The value object must have an method that returns the associated integer number—otherwise a will be raised.
|
|
An optional integer argument to define the base of the numerical system in the argument. If you set the base, the argument must be a string. The argument determines how the string argument is interpreted.
Return Value |
|
Returns an integer number after converting the input argument using its required method for the conversion.
![[Image: lyteCache.php?origThumbUrl=https%3A%2F%2...efault.jpg]](https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FJKDzKWtlaQE%2Fhqdefault.jpg)
Do you still need more background information about Python’s built-in function? No problem, read over the related tutorial.
Related Tutorial: Python’s Built-in Function
5/5 – (1 vote)
![[Image: oct_to_int.gif]](https://blog.finxter.com/wp-content/uploads/2022/12/oct_to_int.gif)
Problem Formulation
Given a string in the octal form:
Code:
s = '0o77'
# or s = '77'How to convert the octal string to an integer in Python?
For example, you want to convert the octal string
Code:
'o10'Code:
8Here are a few other examples:
Octal String |
Decimal
Code:
'0o0'0
Code:
'0o4'4
Code:
'0o10'8
Code:
'0o14'12
Code:
'0o20'16
Code:
'0o77'63
Code:
'0o77777'32767
Oct String to Integer using int() with Base 8
To convert an octal string to an integer, pass the string as a first argument into Python’sbuilt-in
Code:
int()Code:
base=8Code:
int()Code:
int()Here’s a minimal example:
Code:
>>> int('0o77', base=8)
63And here’s how you can convert the additional examples shown above:
Code:
>>> int('0o0', base=8)
0
>>> int('0o4', base=8)
4
>>> int('0o10', base=8)
8
>>> int('0o14', base=8)
12
>>> int('0o20', base=8)
16
>>> int('0o77', base=8)
63
>>> int('0o77777', base=8)
32767You actually don’t need to use the prefix
Code:
'0o'Code:
>>> int('0', base=8)
0
>>> int('4', base=8)
4
>>> int('10', base=8)
8
>>> int('14', base=8)
12
>>> int('20', base=8)
16
>>> int('77', base=8)
63
>>> int('77777', base=8)
32767However, skipping the base but leaving the prefix raises a
Code:
ValueError: invalid literal for int() with base 10: '0o77'Code:
>>> int('0o77')
Traceback (most recent call last): File "<pyshell#16>", line 1, in <module> int('0o77')
ValueError: invalid literal for int() with base 10: '0o77'It assumes that the input string is in base 10 when in fact, it isn’t.
Note: Even though passing a prefixed string Code:
'0o...'Code:
int()Code:
int()In fact, you can specify the base argument as to switch on base guessing—which should be the default behavior anyway!
![[Image: 1f447.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f447.png)
Base Guessing
You can pass a prefixed string
Code:
'0o...'Code:
int()Code:
16Code:
>>> int('0o7', base=8)
7
>>> int('0o7', base=0)
7
>>> int('0o7', 0)
7![[Image: image-40.png]](https://blog.finxter.com/wp-content/uploads/2022/12/image-40.png)
If you don’t have an octal string but a octal number—called a literal—such as
Code:
0xffCode:
int()Code:
>>> 0o743
483
>>> 0o7
7
>>> 0o10
8Code:
Syntax: int(value [, base]) – > intArgument |
Code:
valueA Python object to be converted into an integer number. The value object must have an
Code:
__int__()Code:
TypeError|
Code:
baseAn optional integer argument
Code:
baseCode:
valueCode:
valueCode:
baseReturn Value |
Code:
intReturns an integer number after converting the input argument
Code:
valueCode:
__int__()Do you still need more background information about Python’s built-in
Code:
int()
Related Tutorial: Python’s Built-in Code:
int()
