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.
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_stringinto a list of words using thesplit()method.Check if
word_to_shiftexists within the list of words extracted frominput_string.If
word_to_shiftis not found, return the originalinput_stringunchanged.If
word_to_shiftis found, calculate thecurrent_indexof the word in the list.Calculate the
total_wordsas the length of the word list.Calculate the
shifted_indexfor the new position of the word:For a left shift, subtract the
positionsfrom thecurrent_indexand take the%modulus of thetotal_words.For a right shift, add the
positionsto thecurrent_indexand take the%modulus of thetotal_words.
Perform the word shift by removing the word from its original position using the
pop()method and insert it in the calculatedshifted_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:
Original string
The word we want to shift
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 = 2positions_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