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

Python | Split String and Get Last Element

Summary: Use given_string.rsplit(‘sep’, 1)[-1] to split the string and get the last element. Another approach is to use given_string.rpartition(‘sep’, 1)[-1] Minimal Solution: Problem Formulation 📜Problem: Given a string. How will you split the string and get the last element? Let’s try to understand the given problem with the help of an example: Example 1 In … Read more

Python | Split String after Character

Problem Formulation 📜 Problem: Given a string. How will you split the string after a specific character? Note that there can be different scenarios that can come up when you are trying to split a string after a certain character/sub-string. So, let’s understand the given problem with the help of numerous examples. 🏷️Scenario 1: Splitting … Read more

Python | Split String Every “N” Characters

Summary: One of the easiest ways to split a string after every n character is to use a list comprehension to and slice the string accordingly to extract every n character of the given string and store them in a list. The items of this list represent the required split substrings.A quick Look at the … Read more

Python | Split String at Position

Summary: You can split a given string at a specific position/index using Python’s string–slicing syntax. Minimal Example: Problem Formulation 💬Problem: Given a string, how will you split the given string at any given position? Let’s have a look at a couple of examples that demonstrate what the problem asks you to do: ◈Example 1 The following … Read more

Python | Split String by Whitespace

Summary: Use “given string”.split() to split the given string by whitespace 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 whitespace as a separator/delimiter? Let’s understand the problem with the help of a few examples: … Read more

How to Disable Security Certificate Checks for Requests in Python

Summary: You can disable security certificate checks for requests in Python in two ways: (i) Using Session.verify as False (ii) Setting verify = false inside the request method. Problem Formulation Problem Statement: How to disable security certificate checks for requests in Python? The requests module in Python sends HTTP requests using a specific method to … Read more

Python | Split String into Characters

Summary: Use list(“given string”) to extract each character of the given string and store them as individual items in a list.Minimal Example:print(list(“abc”))# OUTPUT: [‘a’, ‘b’, ‘c’] Problem Formulation Problem: Given a string; How will you split the string into a list of characters? Example: Let’s visualize the problem with the help of an example: input … Read more

How to Print a NumPy Array Without Scientific Notation in Python

Problem Formulation » Problem Statement: Given a NumPy array. How to print the NumPy array without scientific notation in Python? Note: Python represents very small or very huge floating-point numbers in their scientific form. Scientific notation represents the number in terms of powers of 10 to display very large or very small numbers. For example, … Read more

How to Count the Number of Unique Values in a List in Python?

Problem Statement: Consider that you have been given a list in Python. How will you count the number of unique values in the list? Example: Let’s visualize the problem with the help of an example: Given: li = [‘a’, ‘a’, ‘b’, ‘c’, ‘b’, ‘d’, ‘d’, ‘a’]Output: The unique values in the given list are ‘a’, ‘b’, ‘c’, ‘d’. … Read more