## Are Python One-Liners Turing Complete?

Python one-liners are Turing complete since Python is a Turing-complete language and any Python program can be executed as a one-liner using the exec() function, by enclosing the entire program in a string and passing it to exec(). In this article, I slowly build this argument and also provide less “brute-force” ways to build Python … Read more

## Python Integer to Hex — The Ultimate Guide

Working with different number systems and their representations is a common practice in the world of programming. One such conversion involves changing integer values into their corresponding hexadecimal representations. In Python, this transformation can be achieved with ease by utilizing built-in functions and string formatting techniques. Hexadecimal, also known as base-16, is a number system … Read more

## How to Count the Number of Words in a String in Python

Problem Formulation Given a string – sentence. How many words does the string sentence have within it? Examples: INPUT sentence = “Finxter helps you to master Python.” OUTPUT Number of words: 6 INPUT sentence = “””Lorem ipsum dolor sit amet. Consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.””” OUTPUT … Read more

## 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