Python | Split String and Keep Whitespace

⭐Summary: To split a string and keep the delimiters/separators, you can use one of the following methods: (i) Using the regex package and its functions. (ii) Using a list comprehension. Minimal Example Problem Formulation Problem: Given a string in Python. How to split the string and also keep the spaces? Example Consider that there’s a … Read more

Python | Split String when Character Changes

⭐Summary: Use groupby() function from the itertools module to split a string whenever a character changes. Another way to do this is to use the zip function and manipulate the result using a list comprehension. Minimal Example Problem Formulation Problem: Given a string; How will you split the string when the character changes? Example Consider … Read more

Python | Split String Get Element

Summary: Use one of the following ways to split a string to get an element from a string: Minimal Example Problem Formulation 📜Problem: Given a string. How will split the string and get the element you want from the split string? Example 1 Extract the word “world” from the given string. Example 2 This is … Read more

Python | Split String Hyphen

⭐Summary: Use “given string”.split(‘-‘) to split the given string by hyphen and store each word as an individual item in a list. Some other ways to split using hyphen include using a list comprehension and the regex library. Minimal Example Problem Formulation 📜Problem: Given a string, how will you split the string into a list … Read more

Python | Split String by Underscore

⭐Summary: Use “given string”.split(‘_’) to split the given string by underscore and store each word as an individual item in a list. Minimal Example Problem Formulation 📜Problem: Given a string, how will you split the string into a list of words using the underscore as a delimiter? Example Let’s understand the problem with the help … Read more

Python | Split String Between Characters

⭐Summary: The easiest way to split the string between the characters is to slice the string and extract the required split substring. Another effective way to split between characters is to use the re.search method from the regex module. Minimal Example Problem Formulation 📜Problem: Given a string; How will you split the string between characters? … Read more

Python Convert Hex to Base64

💬 Question: How to convert a hexadecimal string such as 02af01ff00 to a normal string using the Base64 format in Python? 👉 Short answer: Use the following fancy one-liner expression to convert the hex string s to a Base64-encoded Python string: base64.b64encode(bytes.fromhex(s)).decode(). For the long answer, keep reading! 🥸 If you’re like me, you may … Read more

Python | Split String After Delimiter

Summary: You can use one of the following methods to split a string after the delimiter – Minimal Example Problem Formulation  📜Problem: Given a string; How will you split the string after the delimiter? The output must only contain the substring after the delimiter. Example Let’s visualize the problem with the help of an example: … Read more

Python | Split String Before Delimiter

Summary: You can use one of the following methods to split a string before the delimiter – Minimal Example Problem Formulation  📜Problem: Given a string; How will you split the string before the delimiter? The output must only contain the substring before the delimiter. Example Let’s visualize the problem with the help of an example: … Read more

Python | Split String Empty Separator

Summary: You can split a string using an empty separator using – (i) list constructor (ii) map+lambda (iii) regex (iv) list comprehension Minimal Example: Problem Formulation 📜Problem: How to split a string using an empty string as a separator? Example: Consider the following snippet – Output: Expected Output: So, this essentially means that when you … Read more

Python | Split String and Convert to Dictionary

Summary: Use a list comprehension to split the given string twice, which returns a list of lists wherein each inner list contains the key-value pairs as individual items. Convert this nested list to a dictionary using the dict constructor. Minimal Example: Problem Formulation Problem: Given a string; How will you split the string using a … Read more