Python – Move Word to Rear end
Last Updated :
08 May, 2023
Sometimes, while working with Python strings, we can have a problem in which we need to find a word and move it to the end of the string. This can have application in many domains, including day-day programming and school programming. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using replace() + “+” operator, The combination of above functions can be used to perform this task. In this, we replace the element with empty string and append the work to the end of string to perform this task.
Python3
test_str = 'Geeksforgeeks is best for geeks '
print ( "The original string is : " + str (test_str))
sub_str = 'best'
res = test_str.replace(sub_str, "") + str (sub_str)
print ( "The string after word removal : " + str (res))
|
Output
The original string is : Geeksforgeeks is best for geeks
The string after word removal : Geeksforgeeks is for geeks best
Time Complexity: O(n), where n is the length of the input string “test_str”.
Space Complexity: O(n)
Method #2 : Using string slicing and find() The combination of above functionalities can also be used to perform this task. In this, we construct the list of string and join it again after performing move using find() and slicing.
Python3
test_str = 'Geeksforgeeks is best for geeks '
print ( "The original string is : " + str (test_str))
sub_str = 'best'
res = test_str[:test_str.find(
sub_str)] + test_str[test_str.find(sub_str) + len (sub_str):] + sub_str
print ( "The string after word removal : " + str (res))
|
Output
The original string is : Geeksforgeeks is best for geeks
The string after word removal : Geeksforgeeks is for geeks best
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3 : Using split(), remove(), append() and join() methods
Python3
test_str = 'Geeksforgeeks is best for geeks '
print ( "The original string is : " + str (test_str))
sub_str = 'best'
x = test_str.split()
x.remove(sub_str)
x.append(sub_str)
res = " " .join(x)
print ( "The string after word removal : " + str (res))
|
Output
The original string is : Geeksforgeeks is best for geeks
The string after word removal : Geeksforgeeks is for geeks best
Method #4: Using the re module
Python3
import re
def move_word_to_rear(test_str, sub_str):
res = re.sub(r '\b' + re.escape(sub_str) + r '\b' , '', test_str)
res + = sub_str
return res
test_str = 'Geeksforgeeks is best for geeks '
sub_str = 'best'
print (move_word_to_rear(test_str, sub_str))
|
Output
Geeksforgeeks is for geeks best
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #5: Using String concatenation:
Python3
def move_word_to_end(string, word):
words = string.split()
result = ""
for w in words:
if w ! = word:
result + = w + " "
result + = word
return result
test_str = 'Geeksforgeeks is best for geeks'
print ( "The original string is : " + str (test_str))
sub_str = 'best'
print (move_word_to_end(test_str, sub_str))
|
Output
The original string is : Geeksforgeeks is best for geeks
Geeksforgeeks is for geeks best
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #6: Using a list comprehension
Python3
def move_word_to_end(string, word):
words = string.split()
if word not in words:
return string
return ' ' .join([w for w in words if w ! = word]) + ' ' + word
test_str = 'Geeksforgeeks is best for geeks'
print ( "The original string is : " + str (test_str))
sub_str = 'best'
print (move_word_to_end(test_str, sub_str))
|
Output
The original string is : Geeksforgeeks is best for geeks
Geeksforgeeks is for geeks best
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #7: Using regular expressions and re.sub()
Step-by-step approach:
- In this approach, we can use the re.sub() method to find and replace the substring at the end of the string.
- Use the pattern “\b(best)\b” to match the word “best” only if it appears as a whole word.
- Replace it with an empty string and append it at the end of the original string.
Below is the implementation of the above approach:
Python3
import re
test_str = 'Geeksforgeeks is best for geeks '
print ( "The original string is : " + str (test_str))
sub_str = 'best'
res = re.sub(r '\b' + sub_str + r '\b' , '', test_str) + sub_str
print ( "The string after word removal : " + str (res))
|
Output
The original string is : Geeksforgeeks is best for geeks
The string after word removal : Geeksforgeeks is for geeks best
Time complexity: O(n), where n is the length of the input string.
Auxiliary space: O(n), as we are creating a new string to store the result.
Method # 8: Using string.partition() and string.replace()
- Initialize the string variable test_str and the substring variable sub_str.
- Use the partition() method to split the test_str into three parts – the part before the first occurrence of sub_str, sub_str itself, and the part after sub_str.
- If sub_str is not found in test_str, then partition() will return the original string as the first part, an empty string as the second part, and another empty string as the third part. In this case, set the first part as the modified string res.
- Otherwise, remove all occurrences of sub_str from the first and third parts of the split string using the replace() method, and concatenate them along with sub_str to form the modified string res.
- Print the original string and the modified string.
Python3
test_str = 'Geeksforgeeks is best for geeks '
print ( "The original string is : " + str (test_str))
sub_str = 'best'
part1, found, part2 = test_str.partition(sub_str)
if not found:
res = part1
else :
part1 = part1.replace(sub_str, "")
part2 = part2.replace(sub_str, "")
res = part1 + sub_str + part2
print ( "The string after word removal : " + str (res))
|
Output
The original string is : Geeksforgeeks is best for geeks
The string after word removal : Geeksforgeeks is best for geeks
Time complexity: O(n), where n is the length of the input string.
Auxiliary space: O(n), for the new string variables created in the process.
Share your thoughts in the comments
Please Login to comment...