Python - Move Word to Rear end


The given problem is required to create a Python program to move a word to the rear end of the given string. So we will be having a Python code with the help of it we will be able to move the word to the specified place.

Understanding the logic for the Problem

The problem at hand is to move a word to the rear end of the given string using Python. So in this article we will use two approaches to perform the given task. In the first approach we will use the replace method of Python. And in the second approach we will use the split, append and remove method of Python. So first we will define the string in which we have to move the word and add that word in the rear end. And keep that word in a separate variable so that we can easily remove it from the string. See the image below for example:

Algorithm

  • Step 1 − Initialize the string for which we have to perform the move operation. Define it with the name input_str. And print it to the console.

  • Step 2 − After that, initialize the word to move from the string at the rear end and name it as move_str.

  • Step 3 − Now we will use the replace method of Python to replace the above word in the given input_str and store the new string in the after_moving variable.

Example

#Code to move word to rear end

# initialize the string
input_str = 'Hello dear friends, You are reading this article on Tutorials Point '

# printing original string
print("Input string is : " + str(input_str))

# initialize the word to move
move_str = 'reading this'

# Move the word to rear end
after_moving = input_str.replace(move_str, "") + str(move_str)

# printing the result after moving the word
print("Output string after moving the word : " + str(after_moving))

Output

Input string is : Hello dear friends, You are reading this article on Tutorials Point 
Output string after moving the word : Hello dear friends, You are  article on Tutorials Point reading this

Algorithm - Another Method

  • Step 1 − Initialize the string for which we have to perform the task and define it with the name input_str. And print this input string to the console.

  • Step 2 − Next, Initialize the word which is to be moved from the given string at the rear end and name it as move_word.

  • Step 3 − Now using the split method we will split the input_str and store it in a separate variable called 'a’.

  • Step 4 − And in the 'a’ string we will remove the move_word using the remove method of Python.

  • Step 5 − After that we will append the removed word from the string using the append method. And using the join method we will join the removed word in the Output string and print the result using Output_str.

Example - Another Method

# Code to show the working

# initialize the string
input_str = 'Tutorials Point is best platform for learners '

# printing original string
print("Input String : " + str(input_str))

# initialize the substring
move_word = 'best'

# move the word to rear end
a = input_str.split()
a.remove(move_word)
a.append(move_word)
Output_str = " ".join(a)
# printing the result
print("Output String : " + str(Output_str))

Output

Input String : Tutorials Point is best platform for learners
Output String : Tutorials Point is platform for learners best

Complexity

The time complexity for both approaches to move the word to the rear end using Python is O(1), because we are performing constant operations on the given string.

Conclusion

As we have successfully created a program to move the word to the rear end in the given string. We have basically used some built-in functions of Python. So with the help of this article you can learn the usage of replace, join, append, remove and split methods of Python.

Updated on: 16-Oct-2023

43 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements