Python oct()函数
oct()
是 Python 的内置函数之一,用于将整数转换为八进制字符串。
oct()函数语法
oct(x)
其中:
x
是一个整数,可以是带符号或无符号整数。
oct()
函数返回一个表示x
的八进制字符串,字符串的前缀为'0o'
。
oct()函数示例
以下是oct()
函数的几个例子:
# 将整数转换为八进制字符串
print(oct(10)) # 输出 '0o12'
print(oct(-10)) # 输出 '-0o12'
print(oct(0)) # 输出 '0o0'
print(oct(0x10)) # 输出 '0o20'
需要注意的是,oct()
函数仅适用于整数类型。如果您尝试将其他类型的对象传递给oct()
函数,将引发TypeError
异常。