![]() |
|
[Tut] Python | Split String by Underscore - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] Python | Split String by Underscore (/thread-100324.html) |
[Tut] Python | Split String by Underscore - xSicKxBot - 12-01-2022 Python | Split String by Underscore Rate this post Summary: Use Code: "given string".split()[b]Minimal Example[/b] Code: text = "Welcome_to_the_world_of_Python"
# Method 1
print(text.split("_")) # Method 2
import re
print(re.split('_', text)) # Method 3
print(list(filter(None, text.split('_')))) # Method 4
print([x for x in re.findall(r'[^_]*|(?!_).*$', text) if x != '']) # OUTPUT: ['Welcome', 'to', 'the', 'world', 'of', 'Python'][b]Problem Formulation[/b] Problem: Given a string, how will you split the string into a list of words using the underscore as a delimiter?[b]Example[/b] Let’s understand the problem with the help of an example. Code: # Input:
text = "Python_Pycharm_Java_Eclipse_Golang_VisualStudio"
# Output:
['Python', 'Pycharm', 'Java', 'Eclipse', 'Golang', 'VisualStudio']Now without any further ado, let’s dive into the numerous ways of solving this problem. [b]Method 1: Using split()[/b] Python’s built-in Code: split()Code: split()Code: 'finxterx42'.split('x')Code: xCode: ['fin', 'ter', '42']Approach: To split a string by underscore, you need to use the underscore as the delimiter. You can simply pass the underscore as a separator to the Code: split('_')Code: Code: text = "Python_Pycharm_Java_Eclipse_Golang_VisualStudio"
print(text.split("_")) # ['Python', 'Pycharm', 'Java', 'Eclipse', 'Golang', 'VisualStudio'] Related Read: Python String split()[b]Method 2: Using re.split()[/b] Another way of separating a string by using the underscore as a separator is to use the Code: re.split()Code: re.split(pattern, string)Code: re.split('a', 'bbabbbab')Code: ['bb', 'bbb', 'b']Approach: You can simply use the Code: re.split()Code: re.split('_', text)Code: '_'Code: Code: import re
text = "Python_Pycharm_Java_Eclipse_Golang_VisualStudio"
print(re.split('_', text)) # ['Python', 'Pycharm', 'Java', 'Eclipse', 'Golang', 'VisualStudio'] Related Read: Python Regex Split[b]Method 3: Using filter()[/b] Python’s built-in Code: filter()Approach: You can use the Code: filter()Code: filter()Code: filter()Code: list()Code: Code: text = "Python_Pycharm_Java_Eclipse_Golang_VisualStudio"
print(list(filter(None, text.split('_')))) # ['Python', 'Pycharm', 'Java', 'Eclipse', 'Golang', 'VisualStudio'] Related Read: Python filter()[b]Method 4: Using re.findall()[/b] The Code: re.findall(pattern, string)Approach: You can use the Code: re.findall()Code: [^_]Code: findallCode: Code: import re
text = "Python_Pycharm_Java_Eclipse_Golang_VisualStudio"
print([x for x in re.findall(r'[^_]*', text) if x != '']) # ['Python', 'Pycharm', 'Java', 'Eclipse', 'Golang', 'VisualStudio'] Related Read: Python re.findall() – Everything You Need to Know[b]Python | Split String by Dot[/b] Now that we have gone through numerous ways of solving the given problem, here’s a similar programming challenge for you to solve. Challenge: You are given a string that contains dots in it. How will you split the string using a dot as a delimiter? Consider the code below and try to split the string by dot. Code: # Input:
text = "stars.moon.sun.sky" # Expected Output:
['stars', 'moon', 'sun', 'sky']Try to solve the problem yourself before looking into the given solutions. Solution: Here are the different methods to split a string by using the dot as a delimiter/separator: Code: text = "stars.moon.sun.sky"
# Method 1
print(text.split(".")) # Method 2
print(list(filter(None, text.split('.')))) # Method 3
import re
print([x for x in re.findall(r'[^.]*|(?!.).*$', text) if x != '']) # Method 4
print(re.split('\\.', text))Hurrah! We have successfully solved the given problem using as many as four different ways. I hope you enjoyed this article and it helps you in your Python coding journey. Please subscribe and stay tuned for more interesting articles! Do you want to master the regex superpower? Check out my new book The Smartest Way to Learn Regular Expressions in Python with the innovative 3-step approach for active learning: (1) study a book chapter, (2) solve a code puzzle, and (3) watch an educational chapter video. |