Python | Split String Convert to Int

Summary: You can split a string and convert it to integer values by either using a list comprehension or Python’s built-in map() function. Minimal Example Problem Formulation πŸ“œProblem: Given a string containing integers separated by a specific delimiter. How will you split  the string using the given delimiter and store the integer strings into a … 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 | Split String by Length

Summary: You can split a string by length by slicing the string into chunks of equal lengths. The indices to slice the string can be deduced using a list comprehension. Minimal Example: Problem Formulation πŸ“œProblem: Given a string, how will you split the string by length? Here’s a related question asked in stack overflow that … Read more

Python decode()

This tutorial explains the Python decode() method with arguments and examples. Before we dive into the Python decode() method, let’s first build some background knowledge about encoding and decoding so you can better understand its purpose. πŸ‘‡ Encoding and Decoding – What Does It Mean? Programs must handle various characters in several languages. Application developers … Read more

Python Int to Hex | String Formatting

πŸ’¬ Question: How to use Python’s string formatting capabilities — f-strings, percentage operator, format(), string.format() — to convert an integer to a hexadecimal string? Lowercase Solution without ‘0x’ Prefix You can convert an integer my_int to a simple lowercase hex string without ‘0x’ prefix by using any of the four string formatting variants—all based on … Read more

Python – Hex String to Bytearray

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

Python | Split String by Multiple Characters/Delimiters

Summary: The most efficient way to split a string using multiple characters is to use Python’s regex library as re.split(“pattern”, “given_string”). An alternate solution is to replace the delimiters in the given string with a whitespace character and then split the string. Minimal Example: Problem Formulation πŸ“œProblem: Given a string. How will you split the … Read more

Python | Split String and Keep Newline

Summary: Use ‘given_string’.splitlines(True) to split the string and also keep the new line character. Minimal Example: Problem Formulation πŸ“œProblem: Given a string. How will you split the string into a list of substrings and keep the new line character intact? Example: Let’s have a look at a test case to understand the given problem. Without … Read more

Python | Split String by Newline

Summary: Use given_string.splitlines() to split a given string by newline. Minimal Example: Problem Formulation πŸ“œProblem: Given a string, How will you split the string into a list of words using newline as a separator/delimiter? Example: Let’s dive into the different ways of solving the given problem. Method 1: Using splitlines Approach: The easiest way to split a … Read more

Python | Split String by Comma and Whitespace

Summary: To split a string by comma and whitespace, you can use a list comprehension that splits the given string by comma and then eliminate the whitespaces from the substrings using the strip method. Minimal Example: Note that there are different scenarios and many other ways of solving the problem. Please read ahead to discover … Read more