How to check whether given value is hex or not in python? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How to check whether given value is hex or not in python?

23rd Oct 2020, 10:21 AM
Dr£@M_c@tcH£r
Dr£@M_c@tcH£r - avatar
5 Answers
+ 11
Use search before asking questions! https://www.sololearn.com/Discuss/1480962/?ref=app
23rd Oct 2020, 10:31 AM
Aakaanksha 💕 [TheBraveCoders]
Aakaanksha 💕 [TheBraveCoders] - avatar
+ 7
Dr£@M_c@tcH£r , in some cases you can not check if it is binary or decimal number: 1, 101, 11, ... In this case you need additional information as a prefix of the binary number like: "0b10110". so 0b indicates that the number is binary, 0x is hexadecimal and 0o is octal.
23rd Oct 2020, 10:36 AM
Lothar
Lothar - avatar
+ 6
I will give you a hint: the int(string,base) function accepts a second parameter (base) which indicates the number system that you want to convert from(like 16 for hex). If the conversion is not correct, it will raize a ValueError exception.
23rd Oct 2020, 10:28 AM
QTWizard
+ 6
Dr£@M_c@tcH£r , please show us your attempt first. Please put your code in playground and link it here. Thanks!
23rd Oct 2020, 10:33 AM
Lothar
Lothar - avatar
+ 4
Issue is resolved. Thanks for the help
27th Oct 2020, 11:45 AM
Dr£@M_c@tcH£r
Dr£@M_c@tcH£r - avatar