Signup/Sign In

Python Program To Replace multiple words with K

In this tutorial, we will learn to replace multiple words with k in a string in Python. Strings in Python are a sequence of characters wrapped in single, double, or triple quotes. A word is a substring. For a given string, we have to replace multiple words in the string with a single letter k.

To solve this problem in Python, we can follow these approaches:

  1. Using join(), split() and list comprehension
  2. Using regex and join()

Approach 1: join(), split() and list comprehension

In this approach, we will first declare a list with words that have to be replaced and then traverse to that words in the string and replace it with the letter k.

The join() method is used to join list elements together. It will return a string after joining the words in the list.

The split() method will be used to get words from the string.

We will use list comprehension, which is a shorter syntax for creating a new list based on the values of an existing list.

Algorithm

Follow the algorithm to understand the approach better.

Step 1- Initialise a string

Step 2- Declare a list of words that should be replaced

Step 3- Declare a variable to store the replacement letter k

Step 4- Use list comprehension and string methods to replace words with the letter k

Step 5- Print the replaced string as the result

Python Program 1

Look at the program to understand the implementation of the above-mentioned approach. In the split() method, we will give space (" ") as the separator to extract words from the string.

test_string = 'Studytonight has best tutorials in python'
print("The original string is : " + str(test_string))
  
word_list = ["best", 'tutorials', 'python']
replace_word = 'k'
replaced_string = ' '.join([replace_word if i in word_list else i for i in test_string.split()])
  
print("String after multiple replace : " + str(replaced_string)) 


The original string is : Studytonight has best tutorials in python
String after multiple replace : Studytonight has k k in k

Approach 1: regex and join()

In this approach, we will use regular expression in Python to find words and then replace those words using join() and list comprehension.

A regular expression is a special sequence of characters that helps us to find other strings or sets of strings, using a specialized syntax held in a pattern.

The join() method is used to join list elements together. It will return a string after joining the words in the list.

We will use list comprehension, which is a shorter syntax for creating a new list based on the values of an existing list.

Algorithm

Follow the algorithm to understand the approach better.

Step 1- Initialise a string

Step 2- Declare a list of words that should be replaced

Step 3- Declare a variable to store the replacement letter k

Step 4- Use list comprehension and string methods to replace words with the letter k

Step 5- Print the replaced string as the result

Python Program 1

Look at the program to understand the implementation of the above-mentioned approach.

import re
test_string = "investment in knowledge pays the best interest"
print("The original string is : " + str(test_string))
  
word_list = ["investment", 'pays', 'interest']
replace_word = 'k'
replaced_string = re.sub("|".join(sorted(word_list, key = len, reverse = True)), replace_word, test_string)

print("String after multiple replace : " + str(replaced_string))


The original string is : investment in knowledge pays the best interest
String after multiple replace : k in knowledge k the best k

Conclusion

In this tutorial, we have learned two different ways for replacing multiple words in a string with a single character k. We have used built-in string methods and list comprehension to get the resultant result.