![]() |
|
[Tut] Python | Split String After Delimiter - 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 After Delimiter (/thread-100313.html) |
[Tut] Python | Split String After Delimiter - xSicKxBot - 11-30-2022 Python | Split String After Delimiter Rate this post Summary: You can use one of the following methods to split a string after the delimiter –
Minimal Example Code: # Given String
chat = "Python Founder: Guido van Rossum"
# Method 1
print(chat.split(':')[1])
# Method 2
print(chat[chat.index(":")+1:])
# Method 3
import re
print(re.findall(":(.*)", chat)[0])
# Method 4
print(chat.partition(':')[2])
# Method 5
print(chat.removeprefix('Python Founder:')) 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: Code: # Input
text = "Subscribe to - Finxter"
# Expected Output
Finxter Method 1: Using split()Approach: First, we will simply split thestring using “-” as the delimiter. Next, to extract the string before the delimiter we will use the index of the required substring. As the Code: split()Code: [1]Code: Code: text = "Subscribe to - Finxter"
res = text.split('-')[1]
print(res)
# FinxterNote: The Code: split() Related Read: Python String split() Method 2: Using String SlicingPrerequisite: String slicing is a concept of carving out a substring from a given string. Use slicing notation Code: s[start:stop:step]Approach: First, we will use the Code: index()Code: Code: text = "Subscribe to - Finxter"
res = text[text.index("-")+1:]
print(res) # FinxterNote: The Code: index() Related Reads: String Slicing in PythonPython String index() Method 3: Using regexThe Code: re.match(pattern, string)Approach: Use the expression Code: re.findall("-(.*)"Code: -Code: Code: import re text = "Subscribe to - Finxter"
print(re.findall("-(.*)", text)[0]) # Finxter Related Read: Python Regex Match Method 4: Using partitionThe Code: partition()Approach: We have used the partition method and used “-” as a separator. As we only need the substring before the delimiter, we have used the index of the required substring on the returned tuple and just printed the third element of the tuple (everything before the separator). Code: Code: text = "Subscribe to - Finxter"
res = text.partition('-')[2]
print(res) # Finxter Related Read: Python String partition() Method 5: Using removeprefixIf you are using Python 3.9 or above then Python facilitates you with the Code: removeprefixCode: removeprefix source: https://docs.python.org/3.9/library/stdtypes.html#str.removeprefixCode: Code: text = "Subscribe to - Finxter"
print(text.removeprefix('Subscribe to -')) # FinxterHurrah! We have successfully solved the given problem using as many as five 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. |