Convert Integer to Octal Format

To convert integer to octal format string in Python, call format(value, format) function and pass the integer for value parameter, and ‘o’ for format parameter.

format() function returns a string with the octal representation of given integer.

Python format() builtin function

Examples

Convert Integer to Octal Format

In this example, we take an integer value and format it to octal, using format() builtin function.

Python Program

</>
Copy
a = 25
result = format(a, 'o')
print('Decimal :', a)
print('Octal : ', result)

Output

Decimal : 25
Octal :  31

Convert Negative Integer to Octal Format

In this example, we take a negative integer value and format it to octal, using format() builtin function.

Python Program

</>
Copy
a = -25
result = format(a, 'o')
print('Decimal :', a)
print('Octal : ', result)

Output

Decimal : -25
Octal :  -31

Conclusion

In this Python Tutorial, we learned how to format an integer to octal using format() function.