Python - Random Replacement of Word in String
Last Updated :
13 Mar, 2023
Given a string and List, replace each occurrence of K word in string with random element from list.
Input : test_str = "Gfg is x. Its also x for geeks", repl_list = ["Good", "Better", "Best"], repl_word = "x" Output : Gfg is Best. Its also Better for geeks Explanation : x is replaced by random replace list values. Input : test_str = "Gfg is x. Its also x for geeks", repl_list = ["Good", "Better", "Nice"], repl_word = "x" Output : Gfg is Best. Its also Nice for geeks Explanation : x is replaced by random replace list values, "Best" and "Nice".
Method #1 : Using shuffle() + loop + replace()
The combination of above functions can be used to solve this problem. In this, we replace each occurrence of K word with random string from list using replace().
Python3
# Python3 code to demonstrate working of
# Random Replacement of Word in String
# Using replace() + shuffle() + loop
from random import shuffle
# initializing string
test_str = "Gfg is val. Its also val for geeks"
# printing original string
print("The original string is : " + str(test_str))
# initializing list
repl_list = ["Good", "Better", "Best"]
# initializing replace word
repl_word = "val"
# shuffing list order
shuffle(repl_list)
for ele in repl_list:
# replacing with random string
test_str = test_str.replace(repl_word, ele, 1)
# printing result
print("String after random replacement : " + str(test_str))
OutputThe original string is : Gfg is val. Its also val for geeks
String after random replacement : Gfg is Best. Its also Better for geeks
Time Complexity: O(n*n) where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n) where n is the number of elements in the list “test_list”.
Method #2 : Using list comprehension + replace() + shuffle()
This is one of the ways in which this task can be performed. In this, we encapsulate entire logic in one-liner using similar functionalities as above method.
Python3
# Python3 code to demonstrate working of
# Random Replacement of Word in String
# Using list comprehension + replace() + shuffle()
from random import shuffle
# initializing string
test_str = "Gfg is val. Its also val for geeks"
# printing original string
print("The original string is : " + str(test_str))
# initializing list
repl_list = ["Good", "Better", "Best"]
# initializing replace word
repl_word = "val"
# one-liner to solve problem
shuffle(repl_list)
res = [test_str.replace(repl_word, ele, 1) for ele in repl_list]
# printing result
print("String after random replacement : " + str(res))
Output
The original string is : Gfg is val. Its also val for geeks String after random replacement : ['Gfg is Good. Its also val for geeks', 'Gfg is Better. Its also val for geeks', 'Gfg is Best. Its also val for geeks']
The Time and Space Complexity for all the methods are the same:
Time Complexity: O(n)
Space Complexity: O(n)
Similar Reads
Case insensitive string replacement in Python We are given a string and our task is to replace a specific word in it, ignoring the case of the letters. This means if we want to replace the word "best" with "good", then all variations like "BeSt", "BEST", or "Best" should also be replaced. For example, if the input is "gfg is BeSt", then the out
3 min read
Python - Random uppercase in Strings Given a String, the task is to write a Python program to convert its characters to uppercase randomly. Examples: Input : test_str = 'geeksforgeeks' Output : GeeksfORgeeks Explanation : Random elements are converted to Upper case characters. Input : test_str = 'gfg' Output : GFg Explanation : Random
4 min read
Word location in String - Python Word location in String problem in Python involves finding the position of a specific word or substring within a given string. This problem can be approached using various methods in Python, such as using the find(), index() methods or by regular expressions with the re module.Using str.find()str.fi
4 min read
Python - Random Range in List We are given a list we need to find random range in list. For example, a = [10, 20, 30, 40, 50, 60] we need to find random ranges so that given output should be random list and it changes in every execution.Using random.samplerandom.sample can randomly pick elements from the list and preserve origin
2 min read
Python | Scramble strings in list Sometimes, while working with different applications, we can come across a problem in which we require to shuffle all the strings in the list input we get. This kind of problem can particularly occur in gaming domain. Let's discuss certain ways in which this problem can be solved. Method #1 : Using
5 min read