Python Articles

Page 837 of 855

How do we use re.finditer() method in Python regular expression?

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 23-Jan-2025 19K+ Views

The re.finditer() method in Python's 're' module finds all occurrences of a specified pattern in a string. It returns an iterator containing match objects for each occurrence. This method is especially useful when you need to determine the position of each match within the input string. Following is the syntax for re.finditer() method. re.finditer(pattern,  string,  flags=0) How to Use 're.finditer()' Method Following are the steps involved in using re.finditer() method. Import the re module: You need to import the regular expression module before using it. ...

Read More

Generate pseudo-random numbers in Python

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 09-Oct-2024 4K+ Views

Many computer applications need random numbers to be generated. However, none of them generate an actual random number. Python, like any other programming language, uses a pseudo-random generator. Python’s random generation is based on the Mersenne Twister algorithm that produces 53-bit precision floats. The technique is fast and thread-safe but not suitable for cryptographic purposes. Python’s standard library contains the random module which defines various functions for handling randomization. The following functions handle random integer number generation. random.seed() − This function initializes the random number generator. ...

Read More

How to convert KivyMD to android apk?

A??? R??
A??? R??
Updated on 29-Aug-2024 1K+ Views

Converting a KivyMD App into an AndroidAPK KivyMD is an ultra-popular framework, an extension of another well-known framework called Kivy, that provides lots of tools and widgets to make modern and beautiful UIs for Android, iOS, and Linux. If you are creating an application with KivyMD and want it to be installed on Android devices, then this conversion must be done. This tutorial will help you convert a KivyMD app into an Android APK. Requirements Have the following tools and software ready before you begin. Python: It is written in Python, so you would have to install the same ...

Read More

How to import other Python files?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 31-May-2024 99K+ Views

The task or process of importing other files in Python allows you to use functions, classes, or variables defined in those files within your current Python script. In this article, we will explore different ways to import other Python files, including built-in modules, user-defined modules, and importing specific functions or variables. By the end of this article, you will have a solid understanding of how to import and use code from other Python files in your current projects. Along the way, we will use code examples followed by lucid explanations to help you understand the above task. Importing a User-Defined ...

Read More

Why do we use question mark literal in Python regular expression?

Md Waqar Tabish
Md Waqar Tabish
Updated on 02-Nov-2023 4K+ Views

Introduction The question mark makes the previous token in the regular expression optional. For example: colou?r is complementary to both colour and colour. A quantifier is what the question mark is known as. You may make multiple tokens optional by combining numerous tokens in parentheses and adding the question mark after the final set of parentheses. Like Nov(ember)? matches between Nov and Nov. Using many question marks, you may create a regular expression matching a wide range of options. Feb(ruary)? 23(rd)? Matches February 23rd, February 23, Feb 23rd and Feb 23. Curly braces can also be used to make something ...

Read More

How can I use Python regex to split a string by multiple delimiters?

Md Waqar Tabish
Md Waqar Tabish
Updated on 02-Nov-2023 26K+ Views

Classes that encompass a collection of characters are known as regular expression classes. One of these classes, d, which matches any decimal digit, will be used. Learning how to split data may be valuable. Data arrives in various kinds and sizes, and it's sometimes not as clean as we'd like. You frequently wish to divide a string by more than one delimiter to make it easier to deal with. The built-in regular expression library re is the easiest way to split a string. The library has a.split() function that works similarly to the above example. This approach stands out since ...

Read More

How to extract date from text using Python regular expression?

Md Waqar Tabish
Md Waqar Tabish
Updated on 02-Nov-2023 12K+ Views

We must first understand some regular expression fundamentals as we will use them. There are various ways to declare patterns in regular expressions, which might make them appear complex but are pretty simple. Regular expressions are patterns that can be used to match strings that adhere to that pattern. You need to read the following article to learn how regular expressions operate. You may commonly extract dates from a given text when learning to code. If you are automating a Python script and need to extract specific numerical figures from a CSV file, if you are a data scientist and ...

Read More

How to use variables in Python regular expression?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 02-Nov-2023 10K+ Views

The following code demonstrates the use of variables in Python regex.The variable cannot contain any special or meta characters or regular expression. We just use string concatenation to create a string.Example import re s = 'I love books' var_name = 'love' result = re.search('(.+)'+var_name+'(.+)',s) print result var_name = 'hate' s2 = 'I hate books' result = re.search('(.+)'+var_name+'(.+)',s2) print resultOutputThis gives the output

Read More

How to Find Factors of Number using Python?

Jayashree
Jayashree
Updated on 09-Sep-2023 12K+ Views

In order to find factors of a number, we have to run a loop over all numbers from 1 to itself and see if it is divisible.Examplenum=int(input("enter a number")) factors=[] for i in range(1,num+1):     if num%i==0:        factors.append(i) print ("Factors of {} = {}".format(num,factors))If i is able to divide num completely, it is added in the list. Finally the list is displayed as the factors of given numberOutputenter a number75 Factors of 75 = [3, 5, 15, 25, 75]

Read More

How to create Python dictionary by enumerate function?

Jayashree
Jayashree
Updated on 09-Sep-2023 5K+ Views

Python enumerate() function takes any iterable as argument and returns enumerate object using which the iterable can be traversed. It contains index and corresponding item in the iterable object like list, tuple, or string.Such enumerate object with index and value is then converted to a dictionary using dictionary comprehension.>>> l1=['aa','bb','cc','dd'] >>> enum=enumerate(l1) >>> enum >>> d=dict((i,j) for i,j in enum) >>> d {0: 'aa', 1: 'bb', 2: 'cc', 3: 'dd'} Learn Python with our step-by-step Python Tutorial.

Read More
Showing 8361–8370 of 8,546 articles
« Prev 1 835 836 837 838 839 855 Next »
Advertisements