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

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

Python Convert String List to Uppercase

Coding Challenge πŸ₯‹ πŸ’¬ Question: How to convert a list of strings to all uppercase in Python? Here are three examples: [‘hello’, ‘Python’, ‘world’] to [‘HELLO’, ‘PYTHON’, ‘WORLD’] [‘a’, ‘b’, ‘c’] to [‘A’, ‘B’, ‘C’] [‘aa aa’, ‘bb$bb’, ‘cc()cc’] to [‘AA AA’, ‘BB$BB’, ‘CC()CC’] There are multiple great ways to accomplish this coding challenge and … Read more

Python Convert String List to Lowercase

Coding Challenge πŸ₯‹ πŸ’¬ Question: How to convert a list of strings to all lowercase in Python? Here are three examples: [‘HELLO’, ‘Python’, ‘world’] to [‘hello’, ‘python’, ‘world’] [‘A’, ‘B’, ‘C’] to [‘a’, ‘b’, ‘c’] [‘AA AA’, ‘BB$BB’, ‘CC()CC’] to [‘aa aa’, ‘bb$bb’, ‘cc()cc’] There are multiple great ways to accomplish this coding challenge and … Read more

Python – Split String After K-th Occurrence of Separator

Coding Challenge πŸ’¬ Question: Given a Python string. How to split the string after the k-th occurrence of the separator (string or character)? In other words: how to ignore the first (k-1) separator occurrences when splitting a string? Here are three examples: πŸ‘‰ Related Tutorial: Python Split String After Second Occurrence Solution You can split … Read more

Python – Split String After Second Occurrence of Separator

Coding Challenge πŸ’¬ Question: Given a Python string. How to split the string after the second occurrence of the separator (string or character)? In other words: how to ignore the first separator occurrence when splitting a string? Here are three examples: ‘a-b-c-d-e-f-g-h’ and sep=’-‘ should be split to [‘a-b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’] … Read more

Python Return String From Function

Do you need to create a function that returns a string but you don’t know how? No worries, in sixty seconds, you’ll know! Go! πŸš€ A Python function can return any object such as a string. To return a string, create the string object within the function body, assign it to a variable my_string, and … Read more

Python Print Dictionary Keys Without “dict_keys”

Problem Formulation and Solution Overview If you print dictionary keys using print(dict.keys()), Python returns a dict_keys object, i.e., a view of the dictionary keys. The representation prints the keys enclosed in a weird dict_keys(…) wrapper text, e.g., dict_keys([1, 2, 3]). Here’s an example: There are multiple ways to change the string representation of the keys, … Read more

How to Remove Text Within Parentheses in a Python String?

Problem Formulation and Solution Overview This article will show you how to remove text within parentheses in Python. To make it more interesting, we have the following running scenario: Rivers Clothing has a CSV file containing all their employees. The format is currently first name (middle name) and last name (for example, Martin (Robert) Simpson). … Read more

Python Print Dictionary Values Without “dict_values”

Problem Formulation and Solution Overview If you print all values from a dictionary in Python using print(dict.values()), Python returns a dict_values object, a view of the dictionary values. The representation prints the values enclosed in a weird dict_values(…) wrapper text, for example: dict_values([1, 2, 3]). Here’s an example: There are multiple ways to change the … Read more

How to Clean and Format Phone Numbers in Python

Problem Formulation and Solution Overview This article will show you how to create, clean up, and format phone numbers in Python. To make it more interesting, we have the following scenario. Right-On Realtors has a Contact Us form on their main website containing several fields. One of these fields is a Phone Number. Upon submission, … Read more