hex() Builtin Function

Python – hex()

Python hex() builtin function converts an integer to a lowercase hexadecimal string prefixed with “0x”.

In this tutorial, you will learn the syntax of any() function, and then its usage with the help of example programs.

Syntax

The syntax of hex() function is

hex(n)

where

ParameterDescription
nAn integer.

hex() function returns a string value.

Examples

1. Convert integer to hexadecimal

In the following program, we take an integer value in n, and convert this integer into a hexadecimal representation.

Python Program

n = 2558
output = hex(n)
print(output)
Run Code Copy

Output

0x9fe

Summary

In this Built-in Functions tutorial, we learned the syntax of hex() built-in function, and how to use this function to convert a given integer into a hexadecimal representation, with examples.

Related Tutorials

Code copied to clipboard successfully 👍