{"id":24790,"date":"2021-11-02T09:47:52","date_gmt":"2021-11-02T04:17:52","guid":{"rendered":"https:\/\/python-programs.com\/?p=24790"},"modified":"2021-11-05T20:36:19","modified_gmt":"2021-11-05T15:06:19","slug":"python-hex-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-hex-method-with-examples\/","title":{"rendered":"Python hex() Method with Examples"},"content":{"rendered":"

In the previous article, we have discussed Python float() Method with Examples<\/a>
\nhex() Method in Python:<\/strong><\/p>\n

The hex() function converts the given number to a hexadecimal value.<\/p>\n

The prefix 0x is always present in the returned string.<\/p>\n

Syntax:<\/strong><\/p>\n

hex(number)<\/pre>\n

Parameters<\/strong><\/p>\n

The hex() function only accepts one argument.<\/p>\n

x – integer number (either an int object or a __index__() method that returns an integer)<\/p>\n

Return Value:<\/strong><\/p>\n

The hex() function converts an integer to its corresponding hexadecimal number and returns it as a string.<\/p>\n

The hexadecimal string returned begins with the prefix 0x, indicating that it is in hexadecimal form.<\/p>\n

Examples:<\/strong><\/p>\n

Example1:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given Number = 20<\/pre>\n

Output:<\/strong><\/p>\n

The given number's{ 20 } Hexadecimal Value =  0x14\r\nThe hex() function Return Type = <class 'str'><\/pre>\n

Note:<\/strong><\/p>\n

The prefix 0x denotes that the outcome is a Hexadecimal string.<\/pre>\n

Example2:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given Number = 4.5<\/pre>\n

Output:<\/strong><\/p>\n

The given float number's{ 4.5 } Hexadecimal Value =  0x1.2000000000000p+2<\/pre>\n

hex() Method with Examples in Python<\/h2>\n