Server Side Programming Articles - Page 1913 of 2650

Regular Expression Patterns in Python

Mohd Mohtashim
Updated on 30-Jan-2020 07:36:37

525 Views

Except for control characters,  (+ ? . * ^ $ ( ) [ ] { } | \), all characters match themselves. You can escape a control character by preceding it with a backslash.Following table lists the regular expression syntax that is available in Python −Sr.No.Pattern & Description1^Matches beginning of line.2$Matches end of line.3.Matches any single character except newline. Using m option allows it to match newline as well.4[...]Matches any single character in brackets.5[^...]Matches any single character not in brackets6re*Matches 0 or more occurrences of preceding expression.7re+Matches 1 or more occurrence of preceding expression.8re?Matches 0 or 1 occurrence of preceding ... Read More

Regular Expression Modifiers in Python

Mohd Mohtashim
Updated on 30-Jan-2020 07:35:43

2K+ Views

Regular expression literals may include an optional modifier to control various aspects of matching. The modifiers are specified as an optional flag. You can provide multiple modifiers using exclusive OR (|), as shown previously and may be represented by one of these −Sr.No.Modifier & Description1re.IPerforms case-insensitive matching.2re.LInterprets words according to the current locale. This interpretation affects the alphabetic group (\w and \W), as well as word boundary behavior(\b and \B).3re.MMakes $ match the end of a line (not just the end of the string) and makes ^ match the start of any line (not just the start of the string).4re.SMakes ... Read More

Search and Replace in Python

Mohd Mohtashim
Updated on 30-Jan-2020 07:32:26

314 Views

One of the most important re methods that use regular expressions is sub.Syntaxre.sub(pattern, repl, string, max=0)This method replaces all occurrences of the RE pattern in string with repl, substituting all occurrences unless max provided. This method returns modified string.Example Live Demo#!/usr/bin/python import re phone = "2004-959-559 # This is Phone Number" # Delete Python-style comments num = re.sub(r'#.*$', "", phone) print "Phone Num : ", num # Remove anything other than digits num = re.sub(r'\D', "", phone) print "Phone Num : ", numOutputWhen the above code is executed, it produces the following result −Phone Num : 2004-959-559 Phone Num : 2004959559Read More

Matching Versus Searching in Python

Mohd Mohtashim
Updated on 30-Jan-2020 07:31:11

1K+ Views

Python offers two different primitive operations based on regular expressions: match checks for a match only at the beginning of the string, while search checks for a match anywhere in the string (this is what Perl does by default).Example Live Demo#!/usr/bin/python import re line = "Cats are smarter than dogs"; matchObj = re.match( r'dogs', line, re.M|re.I) if matchObj:    print "match --> matchObj.group() : ", matchObj.group() else:    print "No match!!" searchObj = re.search( r'dogs', line, re.M|re.I) if searchObj:    print "search --> searchObj.group() : ", searchObj.group() else:    print "Nothing found!!"OutputWhen the above code is executed, it produces the following ... Read More

The search Function in Python

Mohd Mohtashim
Updated on 30-Jan-2020 07:30:15

2K+ Views

This function searches for first occurrence of RE pattern within string with optional flags.SyntaxHere is the syntax for this function −re.search(pattern, string, flags=0)Here is the description of the parameters −Sr.No.Parameter & Description1patternThis is the regular expression to be matched.2stringThis is the string, which would be searched to match the pattern at the beginning of string.3flagsYou can specify different flags using bitwise OR (|). These are modifiers, which are listed in the table below.The re.search function returns a match object on success, none on failure. We use group(num) or groups() function of match object to get matched expression.Sr.No.Match Object Method & Description1group(num=0)This method returns entire match ... Read More

Data Hiding in Python

Sarika Singh
Updated on 18-Aug-2022 12:28:53

7K+ Views

Data hiding is also known as data encapsulation and it is the process of hiding the implementation of specific parts of the application from the user. Data hiding combines members of class thereby restricting direct access to the members of the class. Data hiding plays a major role in making an application secure and more robust Data hiding in Python Data hiding in python is a technique of preventing methods and variables of a class from being accessed directly outside of the class in which the methods and variables are initialized. Data hiding of essential member function prevents the end ... Read More

Overloading Operators in Python

Mohd Mohtashim
Updated on 30-Jan-2020 07:21:12

558 Views

Suppose you have created a Vector class to represent two-dimensional vectors, what happens when you use the plus operator to add them? Most likely Python will yell at you.You could, however, define the __add__ method in your class to perform vector addition and then the plus operator would behave as per expectation −Example Live Demo#!/usr/bin/python class Vector:    def __init__(self, a, b):       self.a = a       self.b = b    def __str__(self):       return 'Vector (%d, %d)' % (self.a, self.b)    def __add__(self, other):       return Vector(self.a + other.a, self.b + other.b) v1 = ... Read More

Base Overloading Methods in Python

Mohd Mohtashim
Updated on 30-Jan-2020 07:22:55

444 Views

Following table lists some generic functionality that you can override in your own classes −Sr.No.Method, Description & Sample Call1__init__ ( self [,args...] )Constructor (with any optional arguments)Sample Call : obj = className(args)2__del__( self )Destructor, deletes an objectSample Call : del obj3__repr__( self )Evaluable string representationSample Call : repr(obj)4__str__( self )Printable string representationSample Call : str(obj)5__cmp__ ( self, x )Object comparisonSample Call : cmp(obj, x)

Destroying Objects (Garbage Collection) in Python

Mohd Mohtashim
Updated on 30-Jan-2020 07:16:43

1K+ Views

Python deletes unneeded objects (built-in types or class instances) automatically to free the memory space. The process by which Python periodically reclaims blocks of memory that no longer are in use is termed Garbage Collection.Python's garbage collector runs during program execution and is triggered when an object's reference count reaches zero. An object's reference count changes as the number of aliases that point to it changes.An object's reference count increases when it is assigned a new name or placed in a container (list, tuple, or dictionary). The object's reference count decreases when it's deleted with del, its reference is reassigned, or ... Read More

Built-In Class Attributes in Python

Vikram Chiluka
Updated on 22-Sep-2022 08:32:28

18K+ Views

In this article, we will explain to you the built-in class attributes in python The built-in class attributes provide us with information about the class. Using the dot (.) operator, we may access the built-in class attributes. The built-in class attributes in python are listed below − Attributes Description __dict__ Dictionary containing the class namespace __doc__ If there is a class documentation class, this returns it. Otherwise, None __name__ Class name. __module__ Module name in which the class is defined. This attribute is "__main__" in interactive mode. __bases__ ... Read More

Advertisements