Python oct() function
The oct() function in Python converts an integer number to an octal string prefixed with “0o”.
Syntax
1 2 3 |
oct(x) |
Parameter
x: This parameter represents an Integer Number
Return
This function returns an octal string.
Example 1
1 2 3 4 5 6 7 |
# Python program explaining # the oct() function num= 128 oct_val = oct(num) print("The octal value for",num,"is",oct_val) |
Output
1 2 3 |
The octal value for 128 is 0o200 |
Example 2
1 2 3 4 5 6 7 |
# Python program explaining # the oct() function print("The octal representation of the ascii value 'r' is " + oct(ord('r'))) # For 23, Hexadecimal is 0x17 print("The octal representation of the binary: " + oct(0x17)) |
Output
1 2 3 4 |
The octal representation of the ascii value 'r' is 0o162 The octal representation of the binary: 0o27 |