www.toppr.com Open in urlscan Pro
34.107.246.5  Public Scan

URL: https://www.toppr.com/guides/python-guide/references/methods-and-functions/methods/built-in/hex/python-hex/
Submission: On October 04 via api from US — Scanned from DE

Form analysis 3 forms found in the DOM

GET https://www.toppr.com/guides/

<form class="fr searchWrapper" role="search" method="get" action="https://www.toppr.com/guides/">
  <button type="submit" class="fr btn-search clickable"><span class="fa fa-search"></span></button>
  <input type="search" name="s" value="" placeholder="Search for a topic" title="Search for a topic">
</form>

GET https://www.toppr.com/guides/

<form class="fr searchWrapper" role="search" method="get" action="https://www.toppr.com/guides/">
  <button type="submit" class="fr btn-search clickable"><span class="fa fa-search"></span></button>
  <input type="search" name="s" value="" placeholder="Search for a topic" title="Search for a topic">
</form>

POST

<form method="POST" class="signupPopup_form">
  <div class="classDropdown">
    <a onclick="showClassDropdown()" class="classDropdownBtn" data-class="12">Class 12</a>
    <div id="myDropdown" class="dropdownContent">
      <a class="dropdown-class-item" data-class="5">Class 5</a><a class="dropdown-class-item" data-class="6">Class 6</a><a class="dropdown-class-item" data-class="7">Class 7</a><a class="dropdown-class-item" data-class="8">Class 8</a><a class="dropdown-class-item" data-class="9">Class 9</a><a class="dropdown-class-item" data-class="10">Class 10</a><a class="dropdown-class-item" data-class="11">Class 11</a><a class="dropdown-class-item" data-class="12">Class 12</a>
    </div>
  </div>
  <div class="signupPopup_form_phoneInput">
    <!-- <div class="signupPopup_form_phoneInput_selectedCountryCode js-selected-country-code">+91</div> -->
    <input type="text" name="phone" maxlength="10" placeholder="Your Phone Number">
    <!-- <ul class="signupPopup_form_phoneInput_countryDropdown js-country-dropdown" data-selected-country-name="India" data-selected-country-code="91">
                    </ul> -->
  </div>
  <div class="signupPopup_footer">
    <button id="start-onboarding" name="btn_submit" class="popup-submit-btn" type="button"> Start Learning Now </button>
  </div>
</form>

Text Content

 * Learn
   * CBSE
     * Class 5 to 12
       * Physics
         * Difference Between in Physics
       * Maths
       * Chemistry
       * Biology
         * Difference Between in Biology
       * English
         * Essays
         * Speech Topics
       * Science
       * Computer Science
         * Computer Fundamentals
         * Programming Methodology
         * Introduction to C++
         * Introduction to Python
         * Programming with Python
       * Beginners Guide to Python
         * Tutorials
         * References
         * Examples
         * Questions
       * Formulas
         * Physics Formulas
         * Chemistry Formulas
         * Maths Formulas
       * Business Studies
       * History
       * Geography
       * Economics
       * Civics
       * Political Science
       * EVS
       * Accountancy
   * Bank
     * Computer Aptitude and Knowledge
     * English Language
     * Reasoning Ability
     * Quantitative Aptitude
     * General Awareness
   * Commerce
     * CA Foundation Course
       * Business Laws
       * Business Economics
       * Business Mathematics and Statistics
       * Principles and Practice of Accounting
       * Commercial Knowledge
       * Business Correspondence and Reporting
     * CS Foundation Course
       * Business Laws – CS
       * Business Economics – CS
       * Business Environment
       * Accounting and Auditing
       * Business Management & Entrepreneurship
       * Business Communication and Ethics
     * CMA Foundation Course
       * Fundamentals of Accounting
       * Fundamentals of Law and Ethics
       * Fundamentals of Business Mathematics & Statistics
       * Fundamentals of Economics and Management – CMA
     * CLAT
       * Legal Aptitude
       * English
       * General Knowledge
       * Mathematics
       * Logical Reasoning
   * Study Materials
     * NCERT Solutions
       * Class 12
         * Physics
         * Chemistry
         * Maths
         * Biology
         * Business Studies
       * Class 11
         * Physics
         * Maths
         * Chemistry
         * Biology
       * Class 10
         * Maths
         * Economics
         * History
         * Geography
         * Political Science
         * Science
       * Class 9
         * Science
         * Maths
         * History
         * Geography
         * Civics
         * Economics
       * Class 8
         * Maths
           * Chapter 1
           * Chapter 2
           * Chapter 3
           * Chapter 4
           * Chapter 5
           * Chapter 6
           * Chapter 7
           * Chapter 8
           * Chapter 9
           * Chapter 10
           * Chapter 11
           * Chapter 12
           * Chapter 13
           * Chapter 14
           * Chapter 15
           * Chapter 16
         * Science
         * Geography
         * Civics
       * Class 7
         * Science
         * Maths
         * History
       * Class 6
         * Maths
         * Science
         * History
         * Geography
         * Civics
       * Class 5
         * Maths
         * EVS
     * CBSE Question Papers
       * Class 10
       * Class 12
 * Exams
   * Engineering Exams
     * JEE Main
     * JEE Advanced
     * UPSEE
     * BITSAT
     * WBJEE
     * VITEEE
     * MHT CET
     * COMEDK
     * NDA
   * Medical Exams
     * NEET
     * AIIMS
     * AP EAMCET
     * TS EAMCET
     * JIPMER
     * BCECE
     * KCET
     * KEAM
   * Commerce Entrance Exam
     * CA Foundation Exam
     * CS Foundation Exam
     * CMA Foundation Exam
   * Bank Exams
     * IBPS Clerk
     * IBPS PO
     * IBPS SO
     * SSC CGL
     * SBI PO
   * Law Entrance Exam
     * CLAT Entrance Exam
     * AILET Entrance Exam
 * Ask
 * Concepts
   * Class 12
     * Class 12 Physics
     * Class 12 Chemistry
     * Class 12 Maths
     * Class 12 Biology
   * Class 10
     * Class 10 Physics
     * Class 10 Chemistry
     * Class 10 Maths
     * Class 10 Biology
     * Class 10 English
   * Class 11
     * Class 11 Physics
     * Class 11 Chemistry
     * Class 11 Maths
     * Class 11 Biology
   * Class 9
     * Class 9 Physics
     * Class 9 Chenistry
     * Class 9 Maths
     * Class 9 Biology
     * Class 9 English
   * Class 8
     * Class 8 Physics
     * Class 8 Chemistry
     * Class 8 Maths
     * Class 8 Biology
     * Class 8 English
   * Class 7
     * Class 7 Physics
     * Class 7 Chemistry
     * Class 7 Maths
     * Class 7 Biology
     * Class 7 English
   * Class 6
     * Class 6 Physics
     * Class 6 Chemistry
     * Class 6 Maths
     * Class 6 Biology
     * Class 6 English
   * Class 5
     * Class 5 Maths
     * Class 5 English

Signup Login

Home > Python Guide > References > Methods and Functions > Python hex()
Methods and Functions


PYTHON HEX()



The python hex function is used to convert an integer to is an equivalent
hexadecimal string. 

The syntax of the python hex function is as follows:

hex(x)



Source Code

Table of content
1 hex() Parameter
2 Return Value from hex()


HEX() PARAMETER

The python hex function takes one argument as the input from the user. 

x- is an integer. The argument is either an int object or it has to define an
_index_() method that gives you an integer


RETURN VALUE FROM HEX()

The python hexadecimal function returns a hexadecimal string with the 0x
format. 

Here’s is an example for the same:

Source Code:

Copy Code Copied Use a different Browser

                    
print("10 in hexadecimal is: ", hex(10))

print("-5 in hexadecimal is", hex(-5))



val = hex(100) # returns string type

print(type(val))



Output



10 in hexadecimal is: 0xa

-5 in hexadecimal is: '-0x5'

<class 'str'>



                

To convert a floating-point number to a hexadecimal number, users can use the
float.hex(). 

Source Code:



Copy Code Copied Use a different Browser

                    
print("The equivalent value of the hexadecimal 3.9 is: ", float.hex(10.1)) # valid


Output:

0x1.4333333333333p+3

                



Errors:

In case of any errors, the TypeError exception is returned. 

What does hex do in Python?

The hex() in python is used to return a number in the hexadecimal format. 

Here are some examples of the python hex().

Example 1: How hex() works?

Copy Code Copied Use a different Browser

                    
number = 435

print(number, 'in hex =', hex(number))



number = 0

print(number, 'in hex =', hex(number))



number = -34

print(number, 'in hex =', hex(number))



returnType = type(hex(number))

print('Return type from hex() is', returnType)

Output

435 in hex = 0x1b3

0 in hex = 0x0

-34 in hex = -0x22

Return type from hex() is <class 'str'>



                

Example 2: Hexadecimal representation of a float

Copy Code Copied Use a different Browser

                    
number = 2.5

print(number, 'in hex =', float.hex(number))



number = 0.0

print(number, 'in hex =', float.hex(number))



number = 10.5

print(number, 'in hex =', float.hex(number))

Output

2.5 in hex = 0x1.4000000000000p+1

0.0 in hex = 0x0.0p+0

10.5 in hex = 0x1.5000000000000p+3



                

How do you write hex in python?

To derive a hexadecimal number, the hex() is used. The given number is converted
to a hexadecimal value using this function.

Program to convert the decimal value to a hexadecimal form

Source Code:

Copy Code Copied Use a different Browser

                    
numb1 = int(input("enter base 10 no\n"))



print("a. Decimal to Hexadecimal ")

print("b. Decimal to Octal")

print("c. Decimal to Binary")

 

print("Enter your choice :- ")

choice = input()
 

if choice is 'a':

 
    print("the number in hexadecimal is" + str(number) +

        " is " + hex(number).lstrip("0x").rstrip("L"))

         

if choice is 'b':

     

    print("the number in octal" + str(number) +

        " is " + oct(number).lstrip("0o").rstrip("L"))

         

if choice is 'c':

     

    print("the number i binary" + str(number) +

        " is "+bin(number).lstrip("0b").rstrip("L"))



Output : 

input variant a)

enter base 10 no

123

Decimal to Hexadecimal 
Decimal to Octal
Decimal to Binary
Enter your choice:- 

a

The number in hexadecimal is 123 is 7b



                

How do you find the hex value in Python?

The hex() function is a built-in Python function for converting an integer value
to its hexadecimal equivalent. Look at some examples below to understand the
working of the hex():

Example 1:

Copy Code Copied Use a different Browser

                    
print("The hexadecimal form of 23 is "

                            + hex(23))
                             

print(" 'a' in hexadecimal is" + hex(ord('a')))

       

print("The hexadecimal form of 3.9 is "

                        + float.hex(3.9))



Output : 


The hexadecimal form of 23 is 0x17

 'a' in hexadecimal is 0x61

The hexadecimal form of 3.9 is 0x1.f333333333333p+1



                

Example 2: The TypeError function is raised when any other datatype rather than
an integer is passed as the argument. 

Source Code:

Copy Code Copied Use a different Browser

                    
print("11.1 hexadecimal format is "

                            + hex(11.1))
Output : 

Traceback (most recent call last):

  File "/home/7e1ac7e34362fd690cdb72cf294502e1.py", line 2, in 

    print("11.1 in hexadecimal is "+hex(11.1))

The 'float' object cannot be read as an integer, which causes a TypeError.


                

How do you decipher hex to text in Python?

To convert hexadecimal in python to text, the user will have to implement the
bytes.fromhex() AND bytes.decode(). To construct a bytes object, use
bytes.fromhex(string) with a hexadecimal string without “0x” at the beginning as
a string. To convert this byte object to an ASCII version of the original
hexadecimal string, call bytes.decode(encoding) using “ASCII” as encoding.

Source Code



Copy Code Copied Use a different Browser

                    
# Remove the leading 0x from the string by slicing it.


hex_string = "0x616263"[2:]


bytes_object = bytes.fromhex(hex_string)

Convert to bytes object

ascii_string = bytes_object.decode("ASCII")

Convert to ASCII representation

print(ascii_string)


OUTPUT

abc

                



Share with friends
FacebookTwitterWhatsApp

Browse

METHODS AND FUNCTIONS

 * Python Standard Library Reference
 * Python Modules
 * Python classmethod()
 * Python CSV: Read and Write CSV files
 * Python Recursion (Recursive Function)
 * Python String isupper()
 * Python Format()
 * Python String isprintable()
 * Python String lstrip()
 * Python String maketrans()

Customize your course in 30 seconds

Which class are you in?

5th
6th
7th
8th
9th
10th
11th
12th
get started
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule
and enjoy fun and interactive classes.
Ashhar Firdausi
IIT Roorkee
Biology
Dr. Nazma Shaik
VTU
Chemistry
Gaurav Tiwari
APJAKTU
Physics
Get Started
Browse

METHODS AND FUNCTIONS

 * Python Standard Library Reference
 * Python Modules
 * Python classmethod()
 * Python CSV: Read and Write CSV files
 * Python Recursion (Recursive Function)
 * Python String isupper()
 * Python Format()
 * Python String isprintable()
 * Python String lstrip()
 * Python String maketrans()



Download the App

Watch lectures, practise questions and take tests on the go.

Download Previous Years Question Papers

Quick Tips and Tricks for Successful Exam Preparation

Download NCERT Notes and Solutions

Learn from video lectures


Customize your course in 30 seconds

Which class are you in?

Class 12
Class 5Class 6Class 7Class 8Class 9Class 10Class 11Class 12

Start Learning Now
No thanks.
Classes Stroke 396
 * Class 5
 * Class 6
 * Class 7
 * Class 8
 * Class 9
 * Class 10
 * Class 11
 * Class 12
 * Class 12+

Boards Stroke 396
 * CBSE
 * ICSE
 * IGCSE
 * Andhra Pradesh
 * Bihar
 * Gujarat
 * Jharkhand
 * Karnataka
 * Kerala

 * Madhya Pradesh
 * Maharashtra
 * Punjab
 * Rajasthan
 * Tamil Nadu
 * Telangana
 * Uttar Pradesh

Exams Stroke 396
 * JEE Main
 * JEE Advanced
 * UPSEE
 * BITSAT
 * TS Eamcet
 * WBJEE
 * VITEEE
 * MHT-CET
 * SAT

 * NEET
 * AIIMS
 * AFMC
 * AP Eamcet
 * COMEDK
 * JIPMER
 * BCECE
 * KCET
 * KEAM

 * NTSE
 * NSO
 * NSTSE
 * KVPY
 * HBBVS
 * IMO
 * IEO
 * IJSO
 * NDA

 * About Us
 * Press
 * Customer Stories
 * Jobs
 * Educators
 * Blog
 * Bytes
 * Contact Us
 * FAQs

 * 
 * Terms of Service
 * Privacy Policy

 * fb_16
 * youtube_16
 * linkedin_16
 * medium_16
 * twitter_16
 * instagram_16


✓
Thanks for sharing!
AddToAny
More…

heap.identify('unique_identifier');