Open In App

Python – Move Word to Rear end

Last Updated : 08 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate working of
# Move Word to Rear end
# Using replace() + "+" operator
 
# initializing string
test_str = 'Geeksforgeeks is best for geeks '
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing Substring
sub_str = 'best'
 
# Move Word to Rear end
# Using replace() + "+" operator
res = test_str.replace(sub_str, "") + str(sub_str)
 
# printing result
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




# Python3 code to demonstrate working of
# Move Word to Rear end
# Using string slicing and find()
 
# initializing string
test_str = 'Geeksforgeeks is best for geeks '
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing Substring
sub_str = 'best'
 
# Move Word to Rear end
# Using string slicing and find()
res = test_str[:test_str.find(
    sub_str)] + test_str[test_str.find(sub_str) + len(sub_str):] + sub_str
 
# printing result
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




# Python3 code to demonstrate working of
# Move Word to Rear end
 
# initializing string
test_str = 'Geeksforgeeks is best for geeks '
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing Substring
sub_str = 'best'
 
# Move Word to Rear end
x = test_str.split()
x.remove(sub_str)
x.append(sub_str)
res = " ".join(x)
# printing result
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):
    # using re.sub() method to remove the substring
    res = re.sub(r'\b' + re.escape(sub_str) + r'\b', '', test_str)
    # adding the substring to the end of the string
    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'
# printing original string
print("The original string is : " + str(test_str))
sub_str = 'best'
print(move_word_to_end(test_str, sub_str))
#This code is contributed by Jyothi pinjala.


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'
# printing original string
print("The original string is : " + str(test_str))
sub_str = 'best'
print(move_word_to_end(test_str, sub_str))
#This code is contributed by Vinay Pinjala.


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
 
# initializing string
test_str = 'Geeksforgeeks is best for geeks '
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing Substring
sub_str = 'best'
 
# Move Word to Rear end
# Using regular expressions and re.sub()
res = re.sub(r'\b' + sub_str + r'\b', '', test_str) + sub_str
 
# printing result
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




# initializing string
test_str = 'Geeksforgeeks is best for geeks '
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing Substring
sub_str = 'best'
 
# split the string using partition() method
part1, found, part2 = test_str.partition(sub_str)
 
# check if sub_str was found in test_str
if not found:
    res = part1
else:
    # remove sub_str from part1 and part2
    part1 = part1.replace(sub_str, "")
    part2 = part2.replace(sub_str, "")
    # concatenate the parts with sub_str in between
    res = part1 + sub_str + part2
 
# printing result
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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads