24 lines
717 B
Markdown
24 lines
717 B
Markdown
|
---
|
|||
|
title: Python Hex Function
|
|||
|
localeTitle: Python十六进制函数
|
|||
|
---
|
|||
|
`hex(x)`是Python 3中的内置函数,用于将整数转换为带有前缀“0x”的小写[十六进制](https://www.mathsisfun.com/hexadecimals.html)字符串。
|
|||
|
|
|||
|
## 论据
|
|||
|
|
|||
|
此函数接受一个参数`x` ,该参数应为整数类型。
|
|||
|
|
|||
|
## 返回
|
|||
|
|
|||
|
此函数返回带有前缀“0x”的小写十六进制字符串。
|
|||
|
|
|||
|
## 例
|
|||
|
```
|
|||
|
print(hex(16)) # prints 0x10
|
|||
|
print(hex(-298)) # prints -0x12a
|
|||
|
print(hex(543)) # prints 0x21f
|
|||
|
```
|
|||
|
|
|||
|
![:rocket:](//forum.freecodecamp.com/images/emoji/emoji_one/rocket.png?v=2 ":火箭:") [运行代码](https://repl.it/CV0S)
|
|||
|
|
|||
|
[官方文件](https://docs.python.org/3/library/functions.html#hex)
|