How to Open a URL in Your Browser From a Python Script?

To open a URL in your standard browser (Win, macOS, Linux) from your Python script, e.g., call webbrowser.open(‘https://google.com’) to open Google. Don’t forget to run import webbrowser first. But you don’t have to install the module because it’s already in Python’s standard library. Example Here’s an example Python script that opens the URL ‘https://finxter.com’: A … Read more

Python Hex String to Little Endian (Bytes/Integer)

What Is Big/Little Endian? Computers perform computations on data that is represented with bytes, i.e., sequences of 0s and 1s. A computer program manipulates data by loading data from memory (RAM, Cache, Disk, SSD), performing computations based on that data, and storing the resulting data back into the memory. A computer program loads and stores … Read more

Python Hex String to Decimal

Question πŸ’¬ How to convert a hexadecimal string to a decimal integer value in Python? Answer Summary To convert a hex string to a decimal integer value, pass the hex string into the int() function with base argument such as int(‘0xfff’, base=16), or eval() function such as eval(‘0xfff’). The result is a decimal integer value … Read more

Python Convert Hex String to Binary

πŸ’¬ Question: How to convert a given hex string such as ‘0xF’ to a binary number such as ‘0b1111′? There are multiple variants of this problem depending on how you want the conversion and from which type to which type. In this article, we’ll go over the different methods from simple to more sophisticated. Let’s … Read more

Hex String to Hex Integer in Python

πŸ’¬ Question: Given a hexadecimal string such as ‘0xf’ in Python. How to convert it to a hexadecimal number in Python so that you can perform arithmetic operations such as addition and subtraction? The hexadecimal string representation with the ‘0x’ prefix indicates that the digits of the numbers do have a hexadecimal base 16. In … Read more

How to Convert Hex String to Bytes in Python?

Problem Formulation Given a string in hexadecimal form: How to convert the hex string to a bytes object in Python? Here are a few examples: Hex String Bytes Object ’01’ b’\x01′ ’04’ b’\x04′ ’08’ b’\x08′ ’01 0a’ b’\x01\n’ ’01 02 0e 0f 0f’ b’\x01\x02\x0e\x0f\x0f’ ‘0f 0f’ b’\x0f\x0f’ Hex String to Bytes using bytes.fromhex(hex_string) To convert … Read more

Python Regex Named Groups

Before we dive into named groups, let’s quickly recap normal capture groups in Python. If you already know normal “unnamed capturing groups” well, feel free to skip the first section and move right away to the next one about named capture groups in Python. πŸ‘‡ What Are Python Regex Groups? A normal Python regex group … Read more

Python Return Error From Function

Problem Formulation πŸ’¬ Question: How do you write a function that returns a real Python error instead of an “error code” such as -1 or None in case something got wrong? For example, you may want to check the function input arguments for having the correct type or length (in the case of iterable arguments) … Read more

Python Return Integer From Function

Do you need to create a function that returns an integer value but you don’t know how? No worries, in sixty seconds, you’ll know! Go! πŸ”₯πŸ”₯πŸ”₯ A Python function can return any object. To return an integer, use the built-in int() function. Or create your own function with an arbitrary expression within the function body … Read more