Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Convert Octal String to Integer in Python

#1
How to Convert Octal String to Integer in Python

5/5 – (1 vote)

Problem Formulation


Given a string in the octal form:

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 'o10' to the decimal integer 8.

Here are a few other examples:


Octal String Decimal
'0o0' 0
'0o4' 4
'0o10' 8
'0o14' 12
'0o20' 16
'0o77' 63
'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’s built-in int() function. Use base=8 as a second argument of the int() function to specify that the given string is an octal number. The int() function will then convert the octal string to an integer with base 10 and return the result.

Here’s a minimal example:

>>> int('0o77', base=8)
63

Examples


And here’s how you can convert the additional examples shown above:

>>> 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)
32767

You actually don’t need to use the prefix '0o' because your second argument already defines unambiguously that the given string is an octal number:

>>> 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)
32767

However, skipping the base but leaving the prefix raises a ValueError: invalid literal for int() with base 10: '0o77':

>>> 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 '0o...' into the int() function is unambiguous, Python’s int() 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 0 to switch on base guessing—which should be the default behavior anyway! ?

Base Guessing


You can pass a prefixed string '0o...' into the int() function and set the base to 0 to switch on base guessing in Python. This uses the prefix to determine the base automatically—without you needing to set it to 16. Yet, you still have to set it to 0 so the benefit is marginal in practice.

>>> int('0o7', base=8)
7
>>> int('0o7', base=0)
7
>>> int('0o7', 0)
7

Converting Octal Literals to Int



If you don’t have an octal string but a octal number—called a literal—such as 0xff, you don’t even need the int() function because Python will automatically convert it to a decimal number:

>>> 0o743
483
>>> 0o7
7
>>> 0o10
8

Background int()


Syntax: int(value [, base]) – > int

Argument value A Python object to be converted into an integer number. The value object must have an __int__() method that returns the associated integer number—otherwise a TypeError will be raised.
base An optional integer argument base to define the base of the numerical system in the value argument. If you set the base, the value argument must be a string. The base argument determines how the string argument is interpreted.
Return Value int Returns an integer number after converting the input argument value using its required __int__() method for the conversion.

YouTube Video

Do you still need more background information about Python’s built-in int() function? No problem, read over the related tutorial.

? Related Tutorial: Python’s Built-in int() Function



https://www.sickgaming.net/blog/2022/12/...in-python/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] Python Int to String with Trailing Zeros xSicKxBot 0 32 12-01-2025, 05:47 PM
Last Post: xSicKxBot
  [Tut] How to Convert MIDI to MP3 in Python – A Quick Overview xSicKxBot 0 2,394 09-02-2023, 02:04 PM
Last Post: xSicKxBot
  [Tut] Wrap and Truncate a String with Textwrap in Python xSicKxBot 0 2,053 09-01-2023, 07:45 PM
Last Post: xSicKxBot
  [Tut] Write a Long String on Multiple Lines in Python xSicKxBot 0 1,500 08-17-2023, 11:05 AM
Last Post: xSicKxBot
  [Tut] 5 Effective Methods to Sort a List of String Numbers Numerically in Python xSicKxBot 0 1,555 08-16-2023, 08:49 AM
Last Post: xSicKxBot
  [Tut] Sort a List, String, Tuple in Python (sort, sorted) xSicKxBot 0 1,694 08-15-2023, 02:08 PM
Last Post: xSicKxBot
  [Tut] F-String Python Hex, Oct, and Bin: Efficient Number Conversions xSicKxBot 0 1,666 03-28-2023, 12:01 PM
Last Post: xSicKxBot
  [Tut] How to Correctly Write a Raw Multiline String in Python: Essential Tips xSicKxBot 0 1,489 03-27-2023, 05:54 PM
Last Post: xSicKxBot
  [Tut] How To Extract Numbers From A String In Python? xSicKxBot 0 1,315 02-26-2023, 02:45 PM
Last Post: xSicKxBot
  [Tut] Python | Split String and Remove newline xSicKxBot 0 1,288 12-16-2022, 10:38 PM
Last Post: xSicKxBot

Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016