Python – String till Substring
Last Updated :
22 Apr, 2023
Sometimes, more than finding a substring, we might need to get the string that is occurring before the substring has been found. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using partition()
The partition function can be used to perform this task in which we just return the part of the partition occurring before the partition word.
Python3
test_string = "GeeksforGeeks is best for geeks"
spl_word = 'best'
print ( "The original string : " + str (test_string))
print ( "The split string : " + str (spl_word))
res = test_string.partition(spl_word)[ 0 ]
print ( "String before the substring occurrence : " + res)
|
Output
The original string : GeeksforGeeks is best for geeks
The split string : best
String before the substring occurrence : GeeksforGeeks is
Time Complexity: O(1) as partition() function takes constant time to split the string at the first occurrence of the given substring.
Auxiliary Space: O(1) as we are only using a few extra variables and not creating any new data structures that would increase space complexity.
Method #2 : Using split()
The split function can also be applied to perform this particular task, in this function, we use the power of limiting the split and then print the former string.
Python3
test_string = "GeeksforGeeks is best for geeks"
spl_word = 'best'
print ( "The original string : " + str (test_string))
print ( "The split string : " + str (spl_word))
res = test_string.split(spl_word)[ 0 ]
print ( "String before the substring occurrence : " + res)
|
Output
The original string : GeeksforGeeks is best for geeks
The split string : best
String before the substring occurrence : GeeksforGeeks is
Time Complexity: O(n), where n is the length of the test_string.
Auxiliary Space: O(1), as we are only using a constant amount of extra space for the variables.
Method #3 : Using find()
Python3
test_string = "GeeksforGeeks is best for geeks"
spl_word = 'best'
print ( "The original string : " + str (test_string))
x = test_string.find(spl_word)
res = test_string[ 0 :x]
print ( "String before the substring occurrence : " + res)
|
Output
The original string : GeeksforGeeks is best for geeks
String before the substring occurrence : GeeksforGeeks is
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 4: Using Regular Expression (re module)
Python3
import re
test_string = "GeeksforGeeks is best for geeks"
spl_word = 'best'
result = re.split(spl_word, test_string, maxsplit = 1 )[ 0 ]
print ( "String before the substring occurrence:" , result)
|
Output
String before the substring occurrence: GeeksforGeeks is
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 5 : using a loop to iterate over the characters of the string.
step-by-step approach
Initialize the string “test_string” as “GeeksforGeeks is best for geeks”.
Initialize the substring “spl_word” as “best”.
Initialize an empty string “res” to store the characters before the substring occurrence.
Loop over each character “char” in the string “test_string”.
Check if the current character “char” is equal to the first character of the substring “spl_word”.
If the current character is the same as the first character of the substring, check if the substring “spl_word” is found starting from the current index of the string “test_string”.
If the substring is found, break out of the loop.
Otherwise, add the current character “char” to the string “res”.
Once the loop is completed, the string “res” will contain the characters before the first occurrence of the substring “spl_word”.
Print the string “res” as the result.
Python3
test_string = "GeeksforGeeks is best for geeks"
spl_word = 'best'
res = ''
for char in test_string:
if char = = spl_word[ 0 ]:
if test_string[test_string.index(char):test_string.index(char) + len (spl_word)] = = spl_word:
break
res + = char
print ( "String before the substring occurrence: " + res)
|
Output
String before the substring occurrence: GeeksforGeeks is
The time complexity of this approach is O(n), where n is the length of the string.
Auxiliary space complexity is O(n), where n is the length of the resulting string.
Please Login to comment...