Skip to content
This repository has been archived by the owner on Apr 1, 2021. It is now read-only.

Article: Python Hex Function #1033

Merged
3 commits merged into from
May 26, 2016
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions Python-Function-Hex.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Python hex(x)

`hex(x)` is a built-in function in Python 3 to convert an integer number to a lowercase [hexadecimal](https://www.mathsisfun.com/hexadecimals.html) string prefixed with “0x”.

## Argument

This function takes one argument, `x`, that should be of integer type.

## Return

This function returns a lowercase hexadecimal string prefixed with "0x".

## Example

```python
print hex(16) # prints 0x10
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Getting this on REPL

Traceback (most recent call last):
  File "python", line 1
    print hex(16)
            ^
SyntaxError: invalid syntax

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are using Python 2 print syntax, use print()

print hex(-298) # prints -0x12a
print hex(543) # prints 0x21f
```

:rocket: [Run Code](https://repl.it/CVCR)

[Official Documentation](https://docs.python.org/3/library/functions.html#hex)