Python Print Octal Without ‘0o’

3.7/5 - (3 votes)

Problem Formulation

If you print an octal number, Python uses the prefix '0o' to indicate that it’s a number in the octal system and not in the decimal system like normal integers.

# 0o52

However, if you already know that the output numbers are octal, you don’t necessarily need the '0o' prefix.

How to print oct numbers without the '0o' prefix?

Method 1: Slicing

To skip the prefix, use slicing and start with index 2 on the octal string. For example, to skip the prefix '0o' on the result of x = oct(42) ='0o52', use the slicing operation x[2:] that results in just the octal number '52' without the prefix '0o'.

x = oct(42)

# 0o52

# 52

Feel free to dive into the oct() built-in function in this video tutorial:

Python oct() Function -- How Does It Work?

But what if you actually want to replace the prefix '0o' with the prefix '00' so that the resulting string has the same length?

Method 2: Slicing + zfill()

The Python string.zfill() method fills the string from the left with '0' characters. In combination with slicing from the third character, you can easily construct an octal string without leading '0o' characters and with leading '0' characters up to the length passed into the string.zfill(length) method.

# 0052

Alternatively, if you want to create a string with 8 characters, use string.zfill(8):

# 00000042

You can learn more about zfill() in this video about Python string methods:

Python String Methods [Ultimate Guide]

Method 3: Negative Octal Numbers

If you need to handle negative octal numbers, the above methods do not work because the oct number now needs to replace the second and third character '-0o'. For example, the octal number oct(-42) is '-0o52'. You cannot simply skip the first two characters to obtain the correct result, can you? At the same time, if you always skipped or replaced the second and third characters, it wouldn’t work for positive numbers either. So what to do?

To print a positive or negative octal without the '0o' or '-0o' prefixes, you can simply use the string.replace('o', '0') method and replace each occurrence of alphabetical 'o' with numerical '0'. The resulting string is mathematically correct because leading '0's don’t change the value of the number.

# Negative Octal
print(oct(-42).replace('o', '0'))
# -0052

# Positive Octal
print(oct(42).replace('o', '0'))
# 0052

Where to Go From Here?

Enough theory. Let’s get some practice!

Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.

To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

You build high-value coding skills by working on practical coding projects!

Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?

🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!