Replace substring in list of strings - Python Last Updated : 22 Apr, 2025 Comments Improve Suggest changes Like Article Like Report We are given a list of strings, and our task is to replace a specific substring within each string with a new substring. This is useful when modifying text data in bulk. For example, given a = ["hello world", "world of code", "worldwide"], replacing "world" with "universe" should result in ["hello universe", "universe of code", "universewide"]. Let's discuss different methods to do this in Python.Using List Comprehension with replace()replace() method replaces occurrences of a substring in a string. Using list comprehension, we can apply it to every string in the list efficiently. Python a = ["hello world", "world of code", "worldwide"] old_substring = "world" new_substring = "universe" res = [s.replace(old_substring, new_substring) for s in a] print(res) Output['hello universe', 'universe of code', 'universewide'] Explanation:The replace() function replaces all occurrences of "world" with "universe" in each string.List comprehension iterates through the list and applies replace() to every string.Using map()map() function applies replace() to each string in the list without using explicit loops. Python a = ["hello world", "world of code", "worldwide"] s1 = "world" s2 = "universe" res = list(map(lambda s: s.replace(s1, s2), a)) print(res) Output['hello universe', 'universe of code', 'universewide'] Explanation:map() applies the replace() function to each string in a.The lambda function takes each string and replaces "world" with "universe".list(map(...)) ensures that the result is stored as a list.Using for Loop and replace()A for loop allows modifying the list step by step, storing results in a new list. Python a = ["hello world", "world of code", "worldwide"] s1 = "world" s2 = "universe" res = [] for s in a: s3 = s.replace(s1, s2) res.append(s3) print(res) Output['hello universe', 'universe of code', 'universewide'] Explanation:The for loop iterates over each string in a.s.replace(s1, s2) replaces "world" with "universe" in each string.The modified string is stored in result using append().Using re.sub() for Pattern-Based Replacementre.sub() function allows replacing substrings based on patterns. Python import re a = ["hello world", "world of code", "worldwide"] s1 = "world" s2 = "universe" res = [re.sub(s1, s2, s) for s in a] print(res) Output['hello universe', 'universe of code', 'universewide'] Explanation:re.sub(pattern, replacement, string) replaces "world" with "universe" in each string.This method is useful when replacing text based on complex patterns rather than exact substrings.Using str.translate() If replacing single characters instead of substrings, str.translate() is a faster alternative. Python a = ["hello world", "world of code", "worldwide"] trans_table = str.maketrans("o", "0") res = [s.translate(trans_table) for s in a] print(res) Output['hell0 w0rld', 'w0rld 0f c0de', 'w0rldwide'] Explanation:str.maketrans("o", "0") creates a mapping to replace "o" with "0".s.translate(trans_table) applies this mapping to each string in a.This method is limited to character replacements rather than full substrings.Related Articles:Python ListsPython StringList Comprehension in PythonPython String replace() MethodPython map() functionPython For Loopsre.sub() - Python RegExPython String translate() Method Comment More infoAdvertise with us Next Article Replace substring in list of strings - Python manjeet_04 Follow Improve Article Tags : Python Python Programs Python list-programs Practice Tags : python Similar Reads Replace Substrings from String List - Python The task of replacing substrings in a list of strings involves iterating through each string and substituting specific words with their corresponding replacements. For example, given a list a = ['GeeksforGeeks', 'And', 'Computer Science'] and replacements b = [['Geeks', 'Gks'], ['And', '&'], ['C 3 min read Python | Substring removal in String list While working with strings, one of the most used application is removing the part of string with another. Since string in itself is immutable, the knowledge of this utility in itself is quite useful. Here the removing of a substring in list of string is performed. Letâs discuss certain ways in which 5 min read Python - Remove substring list from String Our task is to remove multiple substrings from a string in Python using various methods like string replace in a loop, regular expressions, list comprehensions, functools.reduce, and custom loops. For example, given the string "Hello world!" and substrings ["Hello", "ld"], we want to get " wor!" by 3 min read Tokenizing Strings in List of Strings - Python The task of tokenizing strings in a list of strings in Python involves splitting each string into smaller units, known as tokens, based on specific delimiters. For example, given the list a = ['Geeks for Geeks', 'is', 'best computer science portal'], the goal is to break each string into individual 2 min read List of strings in Python A list of strings in Python stores multiple strings together. In this article, weâll explore how to create, modify and work with lists of strings using simple examples.Creating a List of StringsWe can use square brackets [] and separate each string with a comma to create a list of strings.Pythona = 2 min read Python | Remove the given substring from end of string Sometimes we need to manipulate our string to remove extra information from the string for better understanding and faster processing. Given a task in which the substring needs to be removed from the end of the string using Python. Â Â Remove the substring from the end of the string using Slicing In 3 min read Create List of Substrings from List of Strings in Python In Python, when we work with lists of words or phrases, we often need to break them into smaller pieces, called substrings. A substring is a contiguous sequence of characters within a string. Creating a new list of substrings from a list of strings can be a common task in various applications. In th 3 min read Extract List of Substrings in List of Strings in Python Working with strings is a fundamental aspect of programming, and Python provides a plethora of methods to manipulate and extract substrings efficiently. When dealing with a list of strings, extracting specific substrings can be a common requirement. In this article, we will explore five simple and c 3 min read String Repetition and spacing in List - Python We are given a list of strings and our task is to modify it by repeating or adding spaces between elements based on specific conditions. For example, given the list `a = ['hello', 'world', 'python']`, if we repeat each string twice, the output will be `['hellohello', 'worldworld', 'pythonpython']. U 2 min read Python - Remove suffix from string list To remove a suffix from a list of strings, we identify and exclude elements that end with the specified suffix. This involves checking each string in the list and ensuring it doesn't have the unwanted suffix at the end, resulting in a list with only the desired elements.Using list comprehensionUsing 3 min read Like