Friday, December 27, 2024
Google search engine
HomeLanguagesPython | Prefix extraction before specific character

Python | Prefix extraction before specific character

Sometimes, we might have a use case in which we need to find a prefix in a string. But sometimes, the requirement can be something dynamic like a specific input character than a number of elements for the decision of getting prefix. Let’s discuss certain ways in which we can find the prefix of a string before a certain character.

Method #1: Using rsplit()

This method originally performs the task of splitting the string from the rear end rather than the conventional left-to-right fashion. This can though be limited to 1, for solving this particular problem. 

Python3




# Python3 code to demonstrate working of
# Prefix extraction before specific character
# Using rsplit()
 
# initializing string
test_str = &quot
Lazyroar & quot
 
# initializing split character
spl_char = &quot
r & quot
 
# printing original string
print(& quot
       The original string is : & quot
       + str(test_str))
 
# Using rsplit()
# Prefix extraction before specific character
res = test_str.rsplit(spl_char, 1)[0]
 
# printing result
print(& quot
       The prefix string is : & quot
       + str(res))


Output : 

The original string is : Lazyroar
The prefix string is : Geeksfo

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

Method #2: Using rpartition() If we need to solve this particular problem, this inbuilt function is recommended to perform this particular task. This function performs the partition as required just once from the rear end. 

Python3




# Python3 code to demonstrate working of
# Prefix extraction before specific character
# Using rpartition()
 
# initializing string
test_str = &quot
Lazyroar & quot
 
# initializing split character
spl_char = &quot
r & quot
 
# printing original string
print(& quot
       The original string is : & quot
       + str(test_str))
 
# Using rpartition()
# Prefix extraction before specific character
res = test_str.rpartition(spl_char)[0]
 
# printing result
print(& quot
       The prefix string is : & quot
       + str(res))


Output : 

The original string is : Lazyroar
The prefix string is : Geeksfo

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

Method #3 : Using find() method.You can also use index() instead of find().

Python3




# Python3 code to demonstrate working of
# Prefix extraction before specific character
# Using find()
 
# initializing string
test_str = "Lazyroar"
 
# initializing split character
spl_char = "r"
 
# printing original string
print("The original string is : " + str(test_str))
 
# Using find()
# Prefix extraction before specific character
ind=test_str.find(spl_char)
res = test_str[0:ind]
 
# printing result
print("The prefix string is : " + str(res))


Output

The original string is : Lazyroar
The prefix string is : Geeksfo

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

Method #4: Using regex

This method uses python regex to find the prefix of a string before a certain character. It uses the re.split() function to split the string at the specified character.

Python3




import re
 
# initializing string
test_str = "Lazyroar"
 
# initializing split character
spl_char = "r"
 
# printing original string
print("The original string is : " + str(test_str))
 
# Using regex
# Prefix extraction before specific character
res = re.split(spl_char, test_str)[0]
 
# printing result
print("The prefix string is : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy


Output

The original string is : Lazyroar
The prefix string is : Geeksfo

This method uses the re.split() function to split the string at the specified character, which takes O(n) time and the output prefix string is a subset of the input string, so the auxiliary space is O(n)

Method 5: Using split(): 

We can split the string based on the specific character and then extract the first element of the resulting list.

Python3




def prefix_using_split(string, char):
    prefix = string.split(char)[0]
    return prefix
 
original_string = "Lazyroar"
prefix_string = prefix_using_split(original_string, "f")
 
print("The original string is :", original_string)
print("The prefix string is :", prefix_string)


Output

The original string is : Lazyroar
The prefix string is : Geeks

Time Complexity: O(n) (where n is the length of the string)
Space Complexity: O(n) (for the list created by split())

Method #6: Using rfind() and slicing

  • Initialize a string variable test_str with the value “Lazyroar”.
  • Initialize a string variable spl_char with the value “r”.
  • Use the rfind() function on the test_str string to find the last occurrence of the spl_char character. Store this index in the last_index variable.
  • Extract the prefix string from the test_str string up to the last_index index, and store it in the prefix variable using slicing. This means the prefix variable contains all the characters in test_str before the last occurrence of the spl_char character.
  • Print out a message to the console, concatenating the string “The prefix string is : ” with the prefix variable to display the prefix string.

Python3




# initializing string
test_str = "Lazyroar"
 
# initializing split character
spl_char = "r"
 
# finding the last index of the split character
last_index = test_str.rfind(spl_char)
 
# extracting the prefix string
prefix = test_str[:last_index]
 
# printing the prefix string
print("The prefix string is : " + prefix)


Output

The prefix string is : Geeksfo

Time Complexity: O(n) time complexity, where n is the length of the string test_str.
Auxiliary Space: O(1).

RELATED ARTICLES

Most Popular

Recent Comments