re.findall() The expression re.findall() returns all the non-overlapping matches of patterns in a string as a list of strings. Code re.finditer() The expression re.finditer() returns an iterator yielding MatchObject…
group() A group() expression returns one or more subgroups of the match. Code groups() A groups() expression returns a tuple containing all the subgroups of the match. Code groupdict() A groupdict() expression returns a…
So far, we have only heard of Python's powers. Now, we will witness them! Powers or exponents in Python can be calculated using the built-in power function. Call the power function as shown below: or It's also possible…
One of the built-in functions of Python is divmod, which takes two arguments and and returns a tuple containing the quotient of first and then the remainder .
In this challenge, the user enters a string and a substring. You have to print the number of times that the substring occurs in the given string. String traversal will take place from left to right, not from right to…
A newly opened multinational brand has decided to base their company logo on the three most common characters in the company name. They are now trying out various combinations of company names and logos based on this…
There is a horizontal row of N cubes. The length of each cube is given. You need to create a new vertical pile of cubes. The new pile should follow these directions: if is on top of then .
collections.deque() A deque is a double-ended queue. It can be used to add or remove elements from both ends. Deques support thread safe, memory efficient appends and pops from either side of the deque with…
collections.OrderedDict An OrderedDict is a dictionary that remembers the order of the keys that were inserted first. If a new entry overwrites an existing entry, the original insertion position is left unchanged…
collections.namedtuple() Basically, namedtuples are easy to create, lightweight object types. They turn tuples into convenient containers for simple tasks. With namedtuples, you don’t have to use integer indices for…
The defaultdict tool is a container in the collections class of Python. It's similar to the usual dictionary (dict) container, but the only difference is that a defaultdict will have a default value if that key has not…
collections.Counter() A counter is a container that stores elements as dictionary keys, and their counts are stored as dictionary values. Sample Code Task is a shoe shop owner. His shop has number of shoes. He has a…
In this task, we would like for you to appreciate the usefulness of the groupby() function of itertools . To read more about this function, Check this out . You are given a string . Suppose a character '' occurs…
Neo has a complex matrix script. The matrix script is a X grid of strings. It consists of alphanumeric characters, spaces and symbols (!,@,#,$,%,&). To decode the script, Neo needs to read each column and select only…
In Python, a string of text can be aligned left, right and center. .ljust(width) This method returns a left aligned string of length width. .center(width) This method returns a centered string of length width…
itertools.combinations_with_replacement(iterable, r) This tool returns length subsequences of elements from the input iterable allowing individual elements to be repeated more than once. Combinations are emitted in…
itertools.combinations(iterable, r) This tool returns the length subsequences of elements from the input iterable. Combinations are emitted in lexicographic sorted order. So, if the input iterable is sorted, the…
itertools.permutations(iterable[, r]) This tool returns successive length permutations of elements in an iterable. If is not specified or is None, then defaults to the length of the iterable, and all possible full…