- Given string
How to remove all characters in
s after the first occurrence of
Given: - string s = 'hello world', and - empty space character c = ' '. Desired result: 'hello'
Method 1: string.index() + slicing
To remove everything after the first occurrence of character
c, determine the index of
s.index(c). Then pass the result as stop index into a slicing operation, starting from the first character such as in
s[:s.index(c)]. The result is the substring up to the first occurrence of character
# Method 1: string.index() + slicing s = 'hello world' c = ' ' result = s[:s.index(c)] print(result) # hello
Method 2: string.split()
str.split(sep, maxsplit) with
maxsplit set to
1 splits the string into two substrings into a list. It splits the string at the first occurrence of the separator argument
sep. By accessing the first element of this list, you obtain the string with everything before the first occurrence of the
# Method 2: string.split() s = 'hello world' c = ' ' result = s.split(c, 1) print(result) # hello
string.split(delimiter, maxsplit=1) method creates a list of strings, split at the delimiter string. If you split at delimiter character
c, the first element of the split list is the string before the first occurrence of character
c. That’s why the operation
s.split(c, 1) gives you the string with everything removed after the first occurrence of the character
Method 3: Regex
re.findall(pattern, string) method of Python’s regular expression library
re creates a list of strings that match the
pattern in the given
string. By using the pattern
'.*' + c, you match everything up to the first occurrence of the character
c. You take the first element of the returned list with the indexing operation
... and determine all characters but the last one (that is the matching character
c) by using slicing
...[:-1] up to the second last element.
# Method 3: regex import re s = 'hello world' c = ' ' result = re.findall('.*' + c, s)[:-1] print(result) # hello
Where to Go From Here?
Enough theory. Let’s get some practice!
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.
To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
You build high-value coding skills by working on practical coding projects!
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.