Found 10784 Articles for Python

why Python can't define tuples in a function?

Pythonista
Updated on 18-Feb-2020 10:38:44

157 Views

Since Python 3.0, it is no longer possible to define unpacked tuple as a parameter in a function (PEP 3113). It means if you try to define a function as below −def fn(a, (b, c)):    passPython interpreter displays syntax error at first bracket of tuple. Instead, define tuple objects as parameter and unpack inside the function. In following code, two tuple objects representing x and y coordinates of two points are passed as parameters to calculate distance between the two. Before calculating, the tuple objects are unpacked in respective x and y coordinates.def hypot(p1, p2):    x1, y1=p1   ... Read More

How to add space before and after specific character using regex in Python?

Rajendra Dharmkar
Updated on 18-Feb-2020 10:45:44

1K+ Views

The following code shows how space is added before and after the pipe '|' character in given string. Exampleimport re regex = r'\b[|:]\b' s = "abracadabra abraca|dabara | abra cadabra abra ca dabra abra ca dabra abra" print(re.sub(regex, ' \g ', s))OutputThis gives the outputabracadabra abraca | dabara | abra cadabra abra ca dabra abra ca dabra abra

How regular expression alternatives work in Python?

Rajendra Dharmkar
Updated on 08-Sep-2023 13:20:56

730 Views

Python's built−in module 're' provides a powerful tool for working with text data, and regular expressions (regex) are a crucial part of it. However, sometimes you might need to use alternative methods to perform text manipulation tasks that don't involve regex. In this article, we'll explore five code examples that demonstrate how to use alternative methods to perform text manipulation tasks in Python, along with stepwise explanations and illustrations. Regular expressions are an incredibly powerful tool for working with text in Python. They allow us to search, manipulate, and process text in ways that would be otherwise time−consuming and complex. ... Read More

How regular expression grouping works in Python?

Rajendra Dharmkar
Updated on 19-Feb-2020 06:54:55

548 Views

GroupingWe group part of a regular expression by surrounding it with parentheses. This is how we apply operators to the complete group instead of a single character.Capturing GroupsParentheses not only group sub-expressions but they also create backreferences. The part of the string matched by the grouped part of the regular expression, is stored in a backreference. With the help of backreferences,  we reuse parts of regular expressions. In practical applications, we often need regular expressions that can match any one of two or more alternatives. Also, we sometimes want a quantifier to apply to several expressions. All of these can be ... Read More

How regular expression back references works in Python?

Rajendra Dharmkar
Updated on 19-Feb-2020 06:12:06

580 Views

GroupingWe group part of a regular expression by enclosing it in a pair of parentheses. This way we apply operators to the group instead of a single character.Capturing Groups and BackreferencesParentheses not only group sub-expressions but they also create backreferences. The part of the string matched by the grouped part of the regular expression, is stored in a backreference. With the use of backreferences we reuse parts of regular expressions. If sub-expression is placed in parentheses, it can be accessed with \1 or $1 and so on.For example, the regex \b(\w+)\b\s+\1\b matches repeated words, such as tahiti tahiti, because the parentheses ... Read More

What is the groups() method in regular expressions in Python?

Rajendra Dharmkar
Updated on 18-Feb-2020 10:40:57

10K+ Views

The re.groups() methodThis method returns a tuple containing all the subgroups of the match, from 1 up to however many groups are in the pattern. The default argument is used for groups that did not participate in the match; it defaults to None.  In later versions (from 1.5.1 on), a singleton tuple is returned in such cases.example>>> m = re.match(r"(\d+)\.(\d+)", "27.1835") >>> m.groups() ('27', '1835')If we make the decimal place and everything after it optional, not all groups might participate in the match. These groups will default to None unless the default argument is given −>>> m = re.match(r"(\d+)\.?(\d+)?", "27") >>> m.groups()   ... Read More

What are regular expression repetition cases in Python?

Md Waqar Tabish
Updated on 23-Nov-2022 10:04:57

901 Views

We can build regular expressions that recognise repeated character groups using a few special characters. The following metacharacters can be used to search for a character or set of repeated characters. The question mark was the first repeating operator or quantifier developed. It effectively makes it optional by instructing the engine to try matching the previous token 0 or 1 times. The engine is instructed to try matching the previous token zero or more times by the asterisk or star. The plus instructs the engine to make one or more attempts to match the previous token. . Angle brackets are ... Read More

How not to match a character after repetition in Python Regex?

Rajendra Dharmkar
Updated on 08-Sep-2023 13:14:59

239 Views

Regex, short for regular expression, is a powerful tool in Python that allows you to perform complex text pattern matching and manipulation. It's like a Swiss Army knife for string handling, enabling you to slice, dice, and reconfigure text with finesse. However, when it comes to matching characters after repetition, a common pitfall awaits the unwary coder. In this article, we'll delve into this challenge, exploring five distinct code examples, each accompanied by a step−by−step breakdown to illuminate the path through this regex thicket. Example We import the 're' module to access regex functionality. The pattern `r'(\d)+(?=x)'` is constructed. ... Read More

How to match a single character in python using Regular Expression?

Pranav Indukuri
Updated on 08-Nov-2022 11:41:10

1K+ Views

A single character can be matched in python with using regular expression. Regular expression is a sequence of characters that helps you to find a string or a set of string by using a search pattern. Regular expressions are also known as RegEx. Python provides the re module that is used to work with regular expression. In this article, we will look at how to match a single character in python using regular expression. We use ‘.’ special character to match any single character. Using findall() function In the following example, let us assume ‘oats cat pan’ as a string. ... Read More

How to match any non-digit character in Python using Regular Expression?

Pranav Indukuri
Updated on 08-Nov-2022 11:38:59

5K+ Views

A regular expression is a group of characters that allows you to use a search pattern to find a string or a set of strings. RegEx is another name for regular expressions. The re module in Python is used to work with regular expressions. In this article we look how to extract non digit characters in python by using regular expression. We use \D+ regular expression in python to get non digit characters from a string. Where, \D returns a match that does not contain digits. + implies zero or more occurrences of characters. Using findall() function In ... Read More

Advertisements