re.sub() function in python29 Aug 2024 | 3 min read The re.sub() is a function in the re (regular expression) module in Python. It is used to replace all occurrences of a pattern in a string with a new string. The function takes three arguments:
The function returns a new string with all the occurrences of the pattern replaced with the new string. Example: Output: Hello, Universe! In this example, the function re.sub() replaces all occurrences of the string "World" with the string "Universe" in the input string "Hello, World!". It can be useful for tasks such as removing specific characters, replacing specific words with synonyms, or converting the case of characters in a string. Example: Output: 1, 2, 3, 4, , 5, 6, , 7, 8, 9, In this example, the function re.sub() replaces all occurrences of the string "0" with an empty string, effectively removing all occurrences of the digit 0 from the input string. Additionally, re.sub() allows you to use capturing groups in the pattern argument to perform more sophisticated text substitution operations. A capturing group is a group of characters in a pattern that is matched as a single unit and can be referenced in the replacement string using the syntax \1, \2, etc. Example: Here's an example of using re.sub() to replace all occurrences of a specific pattern in a string and retain a portion of the original string in the replacement: Output: banana apple, date cherry In this example, the function re.sub() replaces all occurrences of the pattern "(\w+), (\w+)" with the string "\\2 \\1", which is the result of swapping the two matched capturing groups. The capturing groups are specified using parentheses () in the pattern and are referenced using the syntax \1 and \2 in the replacement string. Example: An example of using re.sub() to convert all the characters in a string to uppercase: Output: HELLO WORLD! In this example, the function re.sub() replaces all occurrences of the pattern "(\w)" with the uppercase version of the matched character. The capturing group (\w) is used to match individual characters in the input string, and the upper() method is used to convert each character to uppercase. The replacement "\\1".upper() combines the matched character with the uppercase conversion, resulting in an output string with all characters in uppercase. Next TopicSklearn Predict Function |
These days, there's a mobile app for everything. From health to education, there is no one field an app isn't in. There comes a thought in mind, and once checked, there is already an app for it. The thing is that there are a lot of...
6 min read
A so LEGB rule, which would be named just after Python scope for names, is used by Python to resolve names. Here's a brief explanation of what each of these phrases means: The code block and body of the any Python function and lambda expression falls under...
3 min read
Computer vision has undergone a revolution because of machine learning and deep learning, which allow computers to comprehend and interpret visual data. Preprocessing the training dataset is one of the key processes in creating efficient machine-learning models for image categorization. Google's open-source TensorFlow machine learning framework...
6 min read
For all the Python developers or any other language developers, it is always fascinating to know about the tricks and tips of the programming language that we are learning. As we all know that Python is one of the most preferred programming languages among developers....
5 min read
How to Design a Hashset in Python? As we know that HashSet is a famous class in Java. HashSet is used to store the values using a hash table. In this tutorial, we will cover HashSet in Python. We will also learn about how we can design...
8 min read
We all know about what ports are and how they are associated with URLs, but many of us may not have ever heard about the port scanner. Basically, the port scanner is an application that is designed for probing a host or server connected with the...
6 min read
xxhash is a Python module for the xxHash library by Yann Collet. xxHash is an Extremely quick Hash calculation, running at RAM speed limits. It effectively finishes the SMHasher test suite, which assesses hash capabilities' impact, scattering, and irregularity characteristics. Code is profoundly convenient, and hashes are...
5 min read
In the following tutorial, we will understand how to recognize License number plates using the Python programming language. We will utilize OpenCV for this project in order to identify the license number plates and the python pytesseract for the characters and digits extraction from the plate....
9 min read
Monkey Patching is an exciting topic of Python. Monkey-patching is a term that refers to modifying a class or module at a run time. In simple words, a class or module's work can be changed at the runtime. Let's understand this concept by real-life example. When we...
2 min read
Python automated testing can be done with the Selenium Python Module. The Selenium Python bindings provide a straightforward API for writing functional and acceptability tests with Selenium WebDriver. Visit Navigating links using the get method - Selenium Python to learn how to open a website using...
2 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India