How to shift a word in a string in Python

Manipulating strings is one of the fundamental operations in Python programming, and one common task is shifting a word within a string. Whether we need to rearrange a word in a sentence or implement a more complex text transformation, understanding how to shift words in a string is a valuable skill. Let’s understand shifting a word in a string with the help of the following illustration.

Input string
Input string
1 of 4

Implementing the shifting operation

Let’s design a shift_word function to shift a specified word left/right within an input string. It will take the following three inputs.

Inputs

  • input_string: It is the input string in which the word will be shifted.

  • word_to_shift: It is the word that needs to be shifted within the string.

  • positions: It is the number of positions by which the word is shifted.

Procedure

  • Split the input_string into a list of words using the split() method.

  • Check if word_to_shift exists within the list of words extracted from input_string.

  • If word_to_shift is not found, return the original input_string unchanged.

  • If word_to_shift is found, calculate the current_index of the word in the list.

  • Calculate the total_words as the length of the word list.

  • Calculate the shifted_index for the new position of the word:

    • For a left shift, subtract the positions from the current_index and take the % modulus of the total_words.

    • For a right shift, add the positions to the current_index and take the % modulus of the total_words.

  • Perform the word shift by removing the word from its original position using the pop() method and insert it in the calculated shifted_index.

  • Join the modified words list back into a string with spaces between words. Return the shifted string.

Output

  • Left shift: The string is shifted left after performing the word shift.

  • Right shift: The string is shifted right after performing the word shift.

Note: For the code below, we must provide the input in the following order:

  1. Original string

  2. The word we want to shift

  3. Negative integer for left shift/ Positive integer for right shift

Code implementation

Let’s look at an implementation of a word shift in Python.

def shift_word(input_str, word, positions):
words = input_str.split()
if word in words:
index = words.index(word)
# Calculate shifted index for left shift (negative positions)
shifted_index = (index + positions) % len(words)
words.insert(shifted_index, words.pop(index))
return ' '.join(words)
else:
return input_str
# input_string = "This is a sample sentence"
input_string = input()
# word_to_shift = "sample"
word_to_shift = input()
# positions_to_shift = 2
positions_to_shift = int(input())
shifted_string = shift_word(input_string, word_to_shift, positions_to_shift)
print("Original string: " + input_string)
print("Shifted string: " + shifted_string)

Enter the input below

The shift_word function shifts a specified word left or right within an input string. It takes the input string, the word to shift, and the number of positions to shift as parameters. The function handles circular shifting using the modulus operator and returns the modified string with the shifted word.

Applications

Shifting a word left or right in a sentence is a string manipulation technique that finds practical use in various domains, including the following:

  • Password generation: Word shifting is widely used for creating secure and complex passwords. Rearranging characters or words within a string helps generate unique and robust passwords, enhancing cybersecurity.

  • Text scrambling in games: In word games and puzzles, text scrambling through word shifting adds an element of challenge and intrigue. Work shifting is popular for creating brain teasers, word jumbles, and interactive puzzles that engage players.

  • Text transformation for natural language processing (NLP): NLP relies on word shifting for text data preprocessing. This technique aids in improving the quality and diversity of training data for NLP models, contributing to better language understanding and generation.

  • Data obfuscation: It involves concealing data while preserving its original format. Word shifting is popular for obscuring data in logs, reports, and other textual records to enhance data privacy and security.

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved