## How to Create a Python Variable At Runtime Based on a User String Input?

💬 Question: Say, you want to dynamically create a variable in your Python script that is named like a string. For example, you want to name the variable after a string value ‘Vladimir’ and assign a value to it so you can access the results using only the variable name Vladimir without the string quotes … Read more

## How to Convert an Octal Escape Sequence in Python – And Vice Versa?

This tutorial will show you how to convert an But let’s quickly recap what an octal escape sequence is in the first place! 👇 What Is An Octal Escape Sequence?💡 An Octal Escape Sequence is a backslash followed by 1-3 octal digits (0-7) such as \150 which encodes the ASCII character ‘h’. Each octal escape … Read more

## How to Convert Octal String to Integer in Python

Problem Formulation Given a string in the octal form: How to convert the octal string to an integer in Python? For example, you want to convert the octal string ‘o10’ to the decimal integer 8. Here are a few other examples: Octal String Decimal ‘0o0’ 0 ‘0o4’ 4 ‘0o10’ 8 ‘0o14’ 12 ‘0o20’ 16 ‘0o77’ … Read more

## Format Integer List to Hex String – Six Pythonic Ways

Problem Formulation 💬 Question: How to create a string of hex digits from a list of integers (0–255) so that each hex digit consists of two digits such as “00”, “01”, …, “fe”, “ff”? Here’s an example input/output pair: In: [0, 1, 2, 3, 255, 254, 253] Out: ‘00010203fffefd’ Method 1: Bytearray The easiest way … Read more

## Python Integer to Hex String (Easy)

💬 Question: How to convert an integer to a hex string in Python? This short tutorial will show you four ways to accomplish this easily and effectively. Let’s dive into the first one right away! 👇 Method 1: hex() The easiest way to convert a decimal integer number x to a hexadecimal string is to … Read more

## Python getattr() and setattr() Nested

Understanding Python’s setattr() and getattr() Functions Next, you’ll learn about the “normal”, non-nested and non-recursive get and set attribute functions. If you already know them well, there’s no need to read this section and you can skip ahead right to the problem formulation and solution. Let’s start with the setattr() function, followed by getattr(). setattr() … Read more

## Python bytes vs bytearray

What’s the Difference Between bytes() and bytearray()? The difference between bytes() and bytearray() is that bytes() returns an immutable and bytearray() returns a mutable object. So you can modify a bytearray but not bytes type. Here’s a minimal example that nicely demonstrates the difference of the two functions: You create two variables a and b. … 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 use range(len()) in Python?

Problem Formulation Have you come across the usage of range(len()) while trying to iterate across all the items of a given iterable? Now, this brings up a couple of questions – (i) Why do we use range(len())? (ii) How do we use range(len())? 📝Solution Generally, range(len()) allows you to iterate across a given iterable/sequence to … Read more

## Python Print Dictionary Keys Without “dict_keys”

Problem Formulation and Solution Overview If you print dictionary keys using print(dict.keys()), Python returns a dict_keys object, i.e., a view of the dictionary keys. The representation prints the keys enclosed in a weird dict_keys(…) wrapper text, e.g., dict_keys([1, 2, 3]). Here’s an example: There are multiple ways to change the string representation of the keys, … Read more