Skip to main content
Taught by Tech Leads

Master pipelines, cloud & AI to become an operational Data Engineer.

DataScientist.fr
Image de String manipulation with replace and re.sub - Interactive tutorial in Python
Python

String manipulation with replace and re.sub - Interactive tutorial in Python

Photo de Romain DE LA SOUCHÈRE

Tech Lead, CTO AXI Technologies

Published on 2 janvier 2025 · 8 min of reading

In the dynamic world of programming, mastering string manipulation is an essential skill. Python, with its wealth of libraries and built-in functions, offers powerful tools for efficiently transforming text. Discover how to harness Python's potential to remove or replace strings and substrings, whether they involve simple substitutions or complex rules. Dive into the realm of re.sub() and explore advanced techniques for even greater precision in your text manipulations.

How to remove or replace a string or substring in Python

To manipulate strings in Python, two common operations are removing and replacing strings or substrings. These manipulations can be performed using built-in methods and simple techniques. In this section, we will explore how to carry out these operations.

Remove a substring

To remove a substring from a main string, a common approach is to use the replace() method by replacing the substring with an empty string. Here is an example:
python
In this example, the substring 'tout ' is removed from the initial string. The replace() method is easy to use and effective for small strings.

Replace a substring

Replacing a substring with another can also be accomplished with replace(). Here’s how:
python
In this example, 'école' is replaced by 'université'. This method is very flexible and allows you to specify the number of occurrences to replace with an optional third argument.

Using regular expressions

For more complex manipulations, regular expressions (regex) are very useful. The re module in Python allows you to remove or replace more sophisticated patterns:
python
With re.sub(), you can replace all occurrences of the pattern 'Python' with 'Programming'. Regular expressions are powerful for complex tasks, such as text processing in various formats.

Conditional replacement

Sometimes it is necessary to replace a substring only if a certain condition is met. This can be done using a simple if condition:
python
In this example, 'beau' is replaced by 'magnifique' only if 'beau' appears in the original text.
By using these techniques, you can manipulate strings in Python in a way that meets your specific needs, whether it's simple removal or conditional replacement.

Setting up multiple replacement rules

When manipulating strings in Python, it is often necessary to apply multiple replacement rules sequentially. This can be particularly useful in situations where you need to clean or transform text based on multiple criteria.

Using a series of replacements

A simple way to apply multiple replacement rules is to use a series of calls to the replace() method. Here’s an example:
python
In this example, a list of tuples remplacements is used to store pairs of strings to be replaced. A simple for loop iterates over these pairs, applying each replacement successively.

Condition-based replacement

For more complex replacements, you can introduce logical conditions. This can be achieved by integrating control structures like if within the loop:
python
Here, each replacement rule is associated with a condition in the form of a lambda function. This allows replacements to occur only when the specified condition is met.

Using a function to centralize replacements

For a more modular approach, encapsulate your replacement rules in a function:
python
This apply_replacements function centralizes the processing, making your code more readable and maintainable.
With these techniques, you can effectively apply multiple replacement rules to your strings in Python, making your code more dynamic and adaptable to various needs.

Leveraging re.sub() to create complex rules

For advanced text manipulations, re.sub() from the re module in Python is a powerful tool that allows you to create complex replacement rules using regular expressions. Let’s explore how to use this function to perform sophisticated transformations on strings.

Pattern-based replacement

With re.sub(), you can replace specific patterns in a string. For example, to replace all occurrences of digits with an asterisk:
python
Here, the pattern \d represents any digit. re.sub() replaces each digit with an asterisk, demonstrating the regex capability to identify and manipulate precise patterns.

Using capture groups

Capture groups allow you to target sub-parts of a pattern for conditional or formatted replacement:
python
In this example, (\d+) captures one or more digits, and \1 in the replacement string refers to the captured group, allowing you to adapt the output format.

Conditional replacement with functions

re.sub() can also use a function to decide the replacement string based on the matching pattern:
python
The function replace_capital takes a match object and returns the corresponding string in uppercase. re.sub() applies this function to each match of the pattern 'python'.
By using re.sub() with patterns and functions, you can set up complex and customized replacement rules, thereby transforming your strings powerfully and flexibly.

Using a callback with re.sub() for even more control

re.sub() offers increased flexibility by allowing the use of callback functions to dynamically determine the replacement text. This feature is particularly useful when the replacement depends on the context or logic surrounding the found pattern.

Implementing a callback function

To use a callback function with re.sub(), you need to define a function that takes a match object and returns the replacement string. Here’s an example:
python
In this example, each occurrence of 'soleil' or 'lune' is transformed into uppercase by the function convert_to_uppercase.

Contextual replacement

Callback functions can also be used to apply contextual replacements by accessing capture groups:
python
In this example, the suffix '-ment' is conditionally added to the words 'rapide' and 'précis', demonstrating how a callback function can modify text based on the captured content.

Benefits of callback functions

Using a callback function with re.sub() offers several advantages:
  • Flexibility: You can apply complex transformations based on business logic.
  • Reusability: Callback functions can be reused in different parts of your code.
  • Readability: By isolating the transformation logic in a separate function, your code becomes clearer and more maintainable.
By integrating callback functions into your replacements with re.sub(), you can achieve precise and sophisticated control over how your strings are manipulated, thus meeting various and complex needs.

Conclusion

Mastering string manipulation in Python is essential for any developer looking to work efficiently with textual data. Throughout this article, we explored various methods and tools for handling strings, ranging from simple replacements to advanced usage of regular expressions.

Summary of techniques

We began by discussing the replace() method, a built-in function in Python that performs basic replacements. It is very useful for simple text manipulation tasks, such as removing or directly replacing substrings.
Next, we examined how to structure multiple replacement rules using loops and conditions. This allows for handling more complex text transformations by sequentially applying different rules, thereby offering increased flexibility in string processing.
The use of regular expressions with re.sub() allowed us to introduce patterns and capture groups, facilitating more sophisticated manipulations. With this approach, it becomes possible to process large volumes of text based on complex criteria while maintaining readable and modular code.
Finally, integrating callback functions with re.sub() provides an additional level of control. By using callbacks, developers can apply conditional and contextual logics to dynamically transform text. This technique is particularly useful when the replacement text depends on external factors or specific calculations.

Practical application

These skills are applicable in many scenarios, whether for data cleaning, text analysis, or dynamic content generation. For example, in the context of processing data from forms or reports, the ability to extract and transform textual information is crucial.
By incorporating these techniques into your development toolbox, you will be better equipped to tackle projects involving complex text manipulations. Whether for everyday programming tasks or large-scale projects, the tools and methods discussed here will provide you with the necessary foundation to effectively handle textual data in Python.
In conclusion, regular practice and experimentation with these methods will deepen your understanding and improve your efficiency as a Python developer.

Want to go further?

This topic is part of our Become a Data Analyst course. Browse the full programme, or get it by email.

Share with

Photo de Romain DE LA SOUCHÈRE

Romain DE LA SOUCHÈRE

Tech Lead, CTO AXI Technologies

Expert Data Engineering et Cloud, Romain affiche plus de 11 ans d'expérience, dont plusieurs années comme Lead Developer sur des solutions Smart Building haute performance. Il y a conçu et mis en production des moteurs de traitement capables d'absorber des centaines de milliers de données de capteurs par minute, ainsi que des bases clusterisées gérant plus de 10 millions de données dynamiques. Certifié Microsoft Azure DevOps Engineer Expert, il maîtrise aussi bien le développement back-end (Python, C#) que le DevOps (Docker, Kubernetes, Terraform) et les agents LLM. Formateur en Python, cloud, DevOps et IA générative appliquée, il forme avec une obsession : Amener chaque apprenant à concevoir et déployer des architectures réellement scalables en production.

» Learn More

Associated trainings

All our trainings
Image de la formation Become a Data Analyst
Become a Data Analyst
6 months
Intermediate
Guarantee