Python String format_map()

Formats the string according to the Format Description Language, passing a mapping object. Minimal Example As you read over the explanations below, feel free to watch our video guide about this particular string method: Syntax and Explanation str.format_map(mapping) Formats the string according to the Format Description Language, passing a mapping object. In that way, it … Read more

Python String index()

Returns the index of the first occurrence of the specified substring, like find() but it raises a ValueError if the substring is not found. Minimal Example As you read over the explanations below, feel free to watch our video guide about this particular string method: Syntax and Explanation str.index(sub[, start[, end]]) Returns the index of … Read more

Python String capitalize()

Return a copy of the string with capitalized first character and lowercased remaining characters. Minimal Example As you read over the explanations below, feel free to watch our video guide about this particular string method: Syntax and Explanation str.capitalize() Creates a new string with capitalized first character, and lowercased remaining characters. Note that this function … Read more

How To Fix TypeError: List Indices Must Be Integers Or Slices, Not ‘Str’?

✯ Overview Problem: Fixing TypeError: list indices must be integers or slices, not str in Python. Example: The following code lists a certain number of transactions entered by the user. Output: Solution: Please go through this solution only after you have gone through the scenarios mentioned below. Bugs like these can be really frustrating! ? But, … Read more

How to Fix TypeError: Can’t Multiply Sequence by non-int of Type ‘float’ In Python?

✯ Overview Problem: Fixing TypeError: Can’t Multiply Sequence by non-int of Type ‘float’ in Python. Example: Consider that you want to calculate the circumference of a circle using the radius entered by the user, as shown below. As you can see above, we encountered a TypeError while executing our code. Bugs like these can be … Read more

Python Regex Split Without Empty String

Problem Formulation Say, you use the re.split(pattern, string) function to split a string on all occurrences of a given pattern. If the pattern appears at the beginning or the end of the string, the resulting split list will contain empty strings. How to get rid of the empty strings automatically? Here’s an example: Note the … Read more

Check if All Characters of a String are Uppercase

Problem Formulation: How to check if all characters of a string are uppercase? Background: A string is a sequence of characters, and is amongst the most commonly used and popular data types in Python. Strings can be enclosed by either single or double quotes and are ‘immutable’, meaning they can’t be changed once created. There … Read more

What’s the Double Colon :: Operator in Python?

Problem Formulation: What does the double colon string[::2] or sequence[3::4] mean in Python? >>> string[::2] You can observe a similar double colon :: for sequences: >>> lst = [1, 2, 3, 4, 5, 6, 7, 8, 9] >>> lst[::2] Answer: The double colon is a special case in Python’s extended slicing feature. The extended slicing … Read more

How To Extract All Emojis From Text in Python?

Summary: This blog explains the various ways one can extract commonly used Emojis embedded within text. Note: All the solutions provided below have been verified using Python 3.9.0b5. Problem Formulation One has a list with normal text words and emojis, all mixed together, as shown below.  How does one extract only the emojis, into a … Read more