Movatterモバイル変換


[0]ホーム

URL:


Open In App

Removing everything after a specific substring in a string involves locating the substring and then extracting only the part of the string that precedes it. For example we are given a string s="Hello, this is a sample string" we need to remove the part of string after a particular substring including the part of substring suppose the given substring is "sample" so we need to remove the part from the substring from the string so the output string will become "Hello, this is a". This can be done using methods like find(),split() orpartition()

Using split()

We can use the split() method to split the string at the desired substring and keep only the part before it.

Python
s="Hello, this is a sample string."substring="sample"# Split the string at the substring and take the first partresult=s.split(substring)[0]print(result)

Output
Hello, this is a

Explanation:

  • Split(substring) method splits the string s into a list at each occurrence of the substring "sample".
  • Indexing with [0], the code retrieves the part of the string before the substring, resulting in "Hello, this is a "

Using find() and String Slicing

Usingfind() to locate the position of the substring and then slice the string to get everything before that position.

Python
s="Hello, this is a sample string."substring="sample"# Find the position of the substring and slice the stringpos=s.find(substring)ifpos!=-1:result=s[:pos]# Everything before the substringelse:result=s# If the substring is not found, keep the original stringprint(result)

Output
Hello, this is a

Explanation:

  • Find(substring) method locates the starting position of the substring "sample" within the string s. If found, it returns the index; otherwise, it returns -1.
  • Substring is found, the string is sliced up to the position of the substring, extracting everything before it. If not found, the original string is kept intact.

Using partition()

partition() method divides the string into three parts: the part before the substring, the substring itself, and the part after. We can discard the part after the substring.

Python
s="Hello, this is a sample string."substring="sample"# Partition the string at the substring and keep only the first partresult=s.partition(substring)[0]print(result)# Output: Hello, this is a

Output
Hello, this is a

Explanation:

  • Partition(substring) method splits the string into a tuple containing three parts: the part before the substring, the substring itself, and the part after.
  • Indexing with [0], the code retrieves the portion of the string before the substring, resulting in "Hello, this is a ".

Improve

Explore

Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp