Rajendra Dharmkar has Published 452 Articles

What is the correct way to define class variables in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 20-Feb-2020 10:19:04

127 Views

Class variables are variables that are declared outside the__init__method. These are static elements, meaning, they belong to the class rather than to the class instances. These class variables are shared by all instances of that class. Example code for class variables Exampleclass MyClass:   __item1 = 123   __item2 = "abc" ... Read More

How to define attributes of a class in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 20-Feb-2020 10:17:04

247 Views

Attributes of a classEverything, almost everything in Python is an object. Every object has attributes and methods. Thus attributes are very fundamental in Python. A class is a construct which is a collection of similar objects. A class also has attributes. There will be a difference between the class attributes ... Read More

How to read and write unicode (UTF-8) files in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 20-Feb-2020 07:59:19

23K+ Views

The io module is now recommended and is compatible with Python 3's open syntax: The following code is used to read and write to unicode(UTF-8) files in PythonExampleimport io with io.open(filename, 'r', encoding='utf8') as f:     text = f.read() # process Unicode text with io.open(filename, 'w', encoding='utf8') as f: ... Read More

How to get the number of capture groups in Python regular expression?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 20-Feb-2020 07:57:19

1K+ Views

The following code gets the number of captured groups using Python regex in given stringExampleimport re m = re.match(r"(\d)(\d)(\d)", "632") print len(m.groups())OutputThis gives the output3

How to write Python regular expression to get all the anchor tags in a webpage?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 20-Feb-2020 07:56:17

151 Views

The following code extracts all tags in the given stringExampleimport re rex = re.compile(r'[\]') l = "this is text1 hi this is text2" print rex.findall(l)Output['', '']

How to use wildcard in Python regular expression?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 20-Feb-2020 07:49:42

2K+ Views

The following code uses the Python regex .()dot character for wildcard which stands for any character other than newline.Exampleimport re rex = re.compile('th.s') l = "this, thus, just, then" print rex.findall(l)OutputThis gives the output['this', 'thus']

How to find all adverbs and their positions in a text using python regular expression?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 20-Feb-2020 07:40:37

334 Views

As per Python documentationIf one wants more information about all matches of a pattern than the matched text, finditer() is useful as it provides match objects instead of strings. If one was a writer who wanted to find all of the adverbs and their positions in some text, he or ... Read More

How to extract numbers from text using Python regular expression?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 20-Feb-2020 07:17:09

1K+ Views

If we want to extract all numbers/digits  individually from given text we use the following regexExampleimport re s = '12345 abcdf 67' result=re.findall(r'\d', s) print resultOutput['1', '2', '3', '4', '5', '6', '7']If we want to extract groups of numbers/digits from given text we use the following regexExampleimport re s = ... Read More

How do we use a delimiter to split string in Python regular expression?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 20-Feb-2020 07:13:40

237 Views

The re.split() methodre.split(pattern, string, [maxsplit=0]):This methods helps to split string by the occurrences of given pattern.Exampleimport re result=re.split(r'a', 'Dynamics') print resultOutput['Dyn', 'mics']Above, we have split the string “Dynamics” by “a”. Method split() has another argument “maxsplit“. It has default value of zero. In this case it does the maximum splits ... Read More

How to escape all special characters for regex in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 20-Feb-2020 07:09:40

1K+ Views

We use re.escape() to escape the special characters −The following code shows how all special characters in given string are escaped using re.escape() method>>> p = '5*(67).89?' >>> re.escape(p) '5\*\(67\)\.89\?'

Advertisements