- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 10784 Articles for Python
![Arnab Chakraborty](https://www.tutorialspoint.com/assets/profiles/123881/profile/60_2940367-1661162113.png)
2K+ Views
Suppose we have a value n. We have to generate all possible well-formed parentheses where n number of opening and closing parentheses are present. So if the value of n = 3, then the parentheses set will be ["()()()", "()(())", "(())()", "(()())", "((()))"]To solve this, we will follow these steps −Define method called genParenthesisRec(). This takes left, right, temp string and result array. initially result array is emptyThe function genParenthesisRec, will work like belowif left = 0 and right := 0, then insert temp into result, and returnif left > 0getParenthesisRec(left – 1, right, temp + “(”, result)if right > ... Read More
![Arnab Chakraborty](https://www.tutorialspoint.com/assets/profiles/123881/profile/60_2940367-1661162113.png)
568 Views
Suppose we have a linked list. We have to remove the Nth node from the end of the list, then return its head. So if the list is like [1, 2, 3, 4, 5, 6] and n = 3, then the returned list will be [1, 2, 3, 5, 6].To solve this, we will follow these steps −If there is no node after head, then return Nonefront := head, back := head, counter := 0 and fount := falsewhile counter
![Arnab Chakraborty](https://www.tutorialspoint.com/assets/profiles/123881/profile/60_2940367-1661162113.png)
2K+ Views
Suppose we have a string containing digits from 2-9 inclusive. We have to return all possible letter combinations that the number could represent. One mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.12a b c3d e f4g h i5j k l6m n o7p q r s8t u v9w x y z*0#For an example, if the given string is “23”, then the possible strings will be [“ad”, “ae”, “af”, “bd”, “be”, “bf”, “cd”, “ce”, “cf”]To solve this, we will follow these steps −Define an array called solve ... Read More
![Arnab Chakraborty](https://www.tutorialspoint.com/assets/profiles/123881/profile/60_2940367-1661162113.png)
5K+ Views
Suppose we have an array of numbers. It stores n integers, there are there elements a, b, c in the array, such that a + b + c = 0. Find all unique triplets in the array which satisfies the situation. So if the array is like [-1, 0, 1, 2, -1, -4], then the result will be [[-1, 1, 0], [-1, -1, 2]]To solve this, we will follow these steps −Sort the array nums, and define an array resfor i in range 0 to length of nums – 3if i > 0 and nums[i] = nums[i - 1], then ... Read More
![Arnab Chakraborty](https://www.tutorialspoint.com/assets/profiles/123881/profile/60_2940367-1661162113.png)
292 Views
Suppose we have a set of n non-negative integers a1, a2, ..., an, each value represents a point at coordinate (i, a[i]). n vertical lines are present in such a way that the two endpoints of line i is at (i, a[i]) and (i, a[0]). We have to find two lines, which together with x-axis forms one container, so our goal is to find two columns where water volume is max. So if the array is like [1, 8, 6, 2, 5, 4, 8, 3, 7], then it will beIn the shaded part, the height is 7 and there are ... Read More
![Mohd Mohtashim](https://www.tutorialspoint.com/assets/profiles/152429/profile/60_230655-1575021953.jpg)
421 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
![Mohd Mohtashim](https://www.tutorialspoint.com/assets/profiles/152429/profile/60_230655-1575021953.jpg)
1K+ 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
![Mohd Mohtashim](https://www.tutorialspoint.com/assets/profiles/152429/profile/60_230655-1575021953.jpg)
196 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
![Mohd Mohtashim](https://www.tutorialspoint.com/assets/profiles/152429/profile/60_230655-1575021953.jpg)
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
![Mohd Mohtashim](https://www.tutorialspoint.com/assets/profiles/152429/profile/60_230655-1575021953.jpg)
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