Python Articles

Page 128 of 855

What is the difference between re.findall() and re.finditer() methods available in Python?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 24-Mar-2026 2K+ Views

This article guides you through the difference between re.findall() and re.finditer() methods available in Python. The re module of Python helps in working with regular expressions. Both the re.findall() and re.finditer() methods are used to search for patterns, but they behave differently. Let us see the differences with simple explanations and examples in this article. Using re.findall() Method The re.findall() method returns a list of all matching patterns found in the string. It searches through the entire string and collects all non-overlapping matches. Example Here is an example to show the usage of the findall() ...

Read More

How to use range in Python regular expression?

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 24-Mar-2026 3K+ Views

In this article, you will find out how to use the range in Python regular expressions. With the help of Python's regular expressions, you can search, match, and manipulate text efficiently. One useful feature in regex is ranges, which helps us define sets of characters within square brackets "[ ]". Range in Regex In Python, a range is used when you want to match characters within a specific group. It is defined inside brackets "[ ]" with the help of a hyphen −. For example: # Matches any digit (0 to 9) pat1 = r"[0-9]" ...

Read More

How to write Python Regular Expression find repeating digits in a number?

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 24-Mar-2026 1K+ Views

In this article, you will learn to write regular expressions in Python to find repeating digits in a given number. Regular expressions are a powerful feature that helps you find matching patterns in strings. Finding repeating digits is useful in many areas like data validation, pattern recognition, and number analysis. For example, in the number 1223, we can identify that "22" contains repeating digits. Understanding the Regex Pattern To find repeating digits, we use the pattern r'(\d)\1+': (\d) − Captures a single digit in a group \1+ − Matches one or more occurrences of the ...

Read More

How to capture an exception raised by a Python Regular expression?

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 24-Mar-2026 1K+ Views

This article provides a guide on how to capture exceptions raised by regular expressions in Python. We will explore simple example programs that demonstrate proper error handling techniques. Regular expressions are useful for matching patterns in text, but they may result in errors like incorrect syntax or mismatches. Python uses exception handling (try-except blocks) to handle these errors gracefully and prevent program crashes. For example, if the given pattern is "[a-z", this is an incorrect pattern that will raise an error because the closing bracket "]" is missing from the character set. Python Regex Exception Type ...

Read More

How to compare regular expressions in Perl and Python?

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 24-Mar-2026 500 Views

This article compares regular expressions in Perl and Python, showing syntax differences and practical examples. Regular expressions are patterns used to match and manipulate strings in both languages. The key difference is syntax: Perl uses $text =~ /pattern/ while Python uses re.search('pattern', text). Let's explore practical examples comparing both approaches. Check if a String Contains a Word Both languages can search for specific words in text, but use different syntax. Python Example Python uses the re.search() function to find patterns in strings ? import re text = "Hello Tutorialspoint" match = re.search(r"Tutorial", ...

Read More

What is difference between \'.\' , \'?\' and \'*\' in Python regular expression?

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 24-Mar-2026 621 Views

This article will discuss the difference between dot '.', question mark '?', and asterisk '*' in Python regular expressions. In regular expressions, '.' means any one character except the newline, '*' means zero or more occurrences of the preceding character or group, and '?' means zero or one occurrence of the preceding character or group. The question mark can also make quantifiers non-greedy in some contexts. Let's understand these symbols using practical examples ? Usage of Dot (.) The dot '.' matches any single character except a newline ? import re # Define ...

Read More

What is Raw String Notation in Python regular expression?

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 24-Mar-2026 6K+ Views

Raw string notation in Python regular expressions helps avoid backslash conflicts by treating backslashes as literal characters rather than escape sequences. This is especially important when writing regex patterns that contain backslashes. The Problem with Regular Strings In regular Python strings, backslashes have special meaning for escape sequences like (newline) and \t (tab). This can interfere with regex patterns that need literal backslashes ? import re # Without raw string - needs double backslashes pattern = "\d+" text = "There are 123 items" matches = re.findall(pattern, text) print("Without raw string:", matches) ...

Read More

How to convert a Python date string to a date object?

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 24-Mar-2026 9K+ Views

In this article, we convert a date string to a date object. We will use the strptime() method to convert a date string to a date object, and we will also use the dateutil module for flexible date parsing. A date string is a date written in text form, like "09 May 2025" or "2025-05-09". To work with dates in Python, we need to convert these strings into date objects. We can achieve this using Python's datetime and dateutil modules. Using the strptime() Function The strptime() method takes a date string as input and converts it into ...

Read More

How to get formatted date and time in Python?

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 24-Mar-2026 113K+ Views

This article will discuss how to format date and time in Python. Python makes it easy to work with date and time with the help of the datetime module. You can format dates and times in different formats as per your needs. Datetime Module of Python The datetime module in Python offers methods for working with date and time values. To use this module, we must first import it using the following import keyword − import datetime The strftime() Function The strftime() function returns a formatted date and time string. It accepts a ...

Read More

How to get current date and time in Python?

Nikitasha Shrivastava
Nikitasha Shrivastava
Updated on 24-Mar-2026 2K+ Views

In this article, we will show you how you can get the current time and date in Python with the help of different functions. Python provides two main modules for working with date and time: the datetime module and the time module. Using the Datetime Module Date and time are not data types in Python, but you can work with them using the datetime module. The datetime module is included in Python, so there is no need to install it separately. Using the now() Method The now() method returns the current local date and time ? ...

Read More
Showing 1271–1280 of 8,549 articles
« Prev 1 126 127 128 129 130 855 Next »
Advertisements