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

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