 Open in App
Not now

# Python – Kth word replace in String

• Last Updated : 26 Jan, 2023

Sometimes, while working with String list, we can have a problem in which we need to replace the most Kth word of string. This problem has many applications in the web development domain. Let’s discuss a way in which this problem can be solved.

Method 1: Using split() + join() This is way in which we can perform this task. In this, we break elements into parts and then return the Kth value and perform the addition of new element using join().

## Python3

 `# Python3 code to demonstrate working of``# Kth word replace in String``# using split() + join()` `# initializing string``test_str ``=` `"GFG ``is` `good"` `# printing original string``print``("The original string ``is` `: " ``+` `test_str)` `# initializing replace string``rep_str ``=` `"best"` `# initializing K``K ``=` `1` `# Kth word replace in String``# using split() + join()``temp ``=` `test_str.split(``' '``)``res ``=` `" ".join(temp[: K]  ``+` `[rep_str] ``+` `temp[K ``+` `1` `:])` `# printing result``print``("The String after performing replace : " ``+` `res)`

Output :

```The original string is : GFG is good
The String after performing replace : GFG best good```

Time Complexity: O(n), where n is the length of the given string.
Auxiliary Space: O(n + m), where n and m are the length of original string and replace string respectively.

Method 2: Using split() and join() methods without slicing

## Python3

 `# Python3 code to demonstrate working of``# Kth word replace in String``# using split() + join()` `# initializing string``test_str ``=` `"GFG is good"` `# printing original string``print``(``"The original string is : "` `+` `test_str)` `# initializing replace string``rep_str ``=` `"best"` `# initializing K``K ``=` `1` `# Kth word replace in String``# using split() + join()``temp ``=` `test_str.split(``' '``)``temp[K]``=``rep_str``res``=``" "``.join(temp)` `# printing result``print``(``"The String after performing replace : "` `+` `res)`

Output

```The original string is : GFG is good
The String after performing replace : GFG best good```

Time Complexity: O(n), where n is the length of the given string.
Auxiliary Space: O(n + m), where n and m are the length of original string and replace string respectively.

Method #3: Using re.sub() and re.findall()

## Python3

 `import` `re` `# initializing string``test_str ``=` `"GFG is good"` `# printing original string``print``(``"The original string is : "` `+` `test_str)` `# initializing replace string``rep_str ``=` `"best"` `# initializing K``K ``=` `1` `# Kth word replace in String``# using re.sub() and re.findall()``temp ``=` `re.findall(r``'\b\w+\b'``, test_str)``temp[K] ``=` `rep_str``res ``=` `re.sub(r``'\b\w+\b'``, ``lambda` `x: temp.pop(``0``), test_str)` `# printing result``print``(``"The String after performing replace : "` `+` `res)``#This code is contributed by Edula Vinay Kumar Reddy`

Output

```The original string is : GFG is good
The String after performing replace : GFG best good
```

This method uses regular expression to find all words in the string and replace the Kth word with the new one using re.sub() function.

Time Complexity: O(n), where n is the length of the given string.
Auxiliary Space: O(n + m), where n and m are the length of original string and replace string respectively.

My Personal Notes arrow_drop_up