Friday, December 27, 2024
Google search engine
HomeLanguagesPython – String till Substring

Python – String till Substring

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




# Python3 code to demonstrate
# String till Substring
# using partition()
 
# initializing string
test_string = "Lazyroar is best for Lazyroar"
 
# initializing split word
spl_word = 'best'
 
# printing original string
print("The original string : " + str(test_string))
 
# printing split string
print("The split string : " + str(spl_word))
 
# using partition()
# String till Substring
res = test_string.partition(spl_word)[0]
 
# print result
print("String before the substring occurrence : " + res)


Output

The original string : Lazyroar is best for Lazyroar
The split string : best
String before the substring occurrence : Lazyroar 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




# Python3 code to demonstrate
# String till Substring
# using split()
 
# initializing string
test_string = "Lazyroar is best for Lazyroar"
 
# initializing split word
spl_word = 'best'
 
# printing original string
print("The original string : " + str(test_string))
 
# printing split string
print("The split string : " + str(spl_word))
 
# using split()
# String till Substring
res = test_string.split(spl_word)[0]
 
# print result
print("String before the substring occurrence : " + res)


Output

The original string : Lazyroar is best for Lazyroar
The split string : best
String before the substring occurrence : Lazyroar 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




# Python3 code to demonstrate
# String till Substring
# using find()
# you can also use index() instead of find()
 
# initializing string
test_string = "Lazyroar is best for Lazyroar"
 
# initializing substring
spl_word = 'best'
 
# printing original string
print("The original string : " + str(test_string))
 
 
# String till Substring
x=test_string.find(spl_word)
res=test_string[0:x]
 
 
# print result
print("String before the substring occurrence : " + res)


Output

The original string : Lazyroar is best for Lazyroar
String before the substring occurrence : Lazyroar is 

Time Complexity: O(n)
Auxiliary Space: O(n)

Method 4: Using Regular Expression (re module)

Python3




import re
 
#initializing string
test_string = "Lazyroar is best for Lazyroar"
 
#initializing substring
spl_word = 'best'
 
#using regular expression
result = re.split(spl_word, test_string, maxsplit=1)[0]
 
#printing result
print("String before the substring occurrence:", result)


Output

String before the substring occurrence: Lazyroar 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 “Lazyroar is best for Lazyroar”.
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




# Initializing string
test_string = "Lazyroar is best for Lazyroar"
 
# Initializing substring
spl_word = 'best'
 
# Initializing an empty string to store the characters before the substring occurrence
res = ''
 
# Looping over the characters of the string
for char in test_string:
    # Checking if the current character is the first character of the substring
    if char == spl_word[0]:
        # Checking if the substring is found
        if test_string[test_string.index(char):test_string.index(char)+len(spl_word)] == spl_word:
            break
    res += char
 
# Printing the result
print("String before the substring occurrence: " + res)


Output

String before the substring occurrence: Lazyroar 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.

RELATED ARTICLES

Most Popular

Recent Comments