Rajendra Dharmkar has Published 453 Articles

What is the difference between re.match(), re.search() and re.findall() methods in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 02-Nov-2023 07:01:15

4K+ Views

re.match(), re.search(), and re.findall() are methods of the Python module re (Python Regular Expressions). The re.match() method The re.match() method finds match if it occurs at start of the string. For example, calling match() on the string ‘TP Tutorials Point TP’ and looking for a pattern ‘TP’ will match.   ... Read More

How to use variables in Python regular expression?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 02-Nov-2023 06:51:00

7K+ 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' ... Read More

How can we do date and time math in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 02-Nov-2023 02:07:05

2K+ Views

It is very easy to do date and time maths in Python using timedelta objects. Whenever you want to add or subtract to a date/time, use a datetime.datetime(), then add or subtract datetime.timedelta() instances. A timedelta object represents a duration, the difference between two dates or times. The timedelta constructor ... Read More

How to convert Python date in JSON format?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 02-Nov-2023 01:50:21

2K+ Views

There is no standard JSON format for dates. Although JavaScript does have a standard date format that is human readable, sorts correctly, includes fractional seconds(which can help re-establish chronology) and  conforms to ISO 8601. You can convert a Python date to the JS date format using the strftime function and ... Read More

How to convert Python date string mm/dd/yyyy to datetime?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 28-Sep-2023 02:02:47

10K+ Views

In Python, you can convert a string to date object using the strptime() function. Provide the date string and the format in which the date is specified.  Example import datetime date_str = '29/12/2017' # The date - 29 Dec 2017 format_str = '%d/%m/%Y' # The format datetime_obj = datetime.datetime.strptime(date_str, ... Read More

How to store and retrieve a date into MySQL database using Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 28-Sep-2023 01:40:28

4K+ Views

To insert a date in a MySQL database, you need to have a column of Type date or datetime in your table. Once you have that, you'll need to convert your date in a string format before inserting it to your database. To do this, you can use the datetime ... Read More

How can we create recursive functions in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 28-Sep-2023 01:19:18

3K+ Views

Recursion is a programming technique, in which a function calls itself one or more times in its body. Usually, it is returning the return value of this function call. If a function definition follows recursion, we call this function a recursive function. A recursive function has to be terminated before ... Read More

How can we overload a Python function?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 28-Sep-2023 01:15:57

3K+ Views

In Python, you can define a method in such a way that there are multiple ways to call it. Depending on the function definition, it can be called with zero, one, two or more parameters. This is known as method overloading. In the given code, there is a class with ... Read More

How to list non-hidden files and directories in windows using Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 11-Sep-2023 17:18:17

657 Views

When coming across and dealing with file and directory operations within the Windows environment using Python, it's not uncommon to come across the hidden files and directories. These hidden files and folders usually are system files and are purposefully kept out of sight from users. Among these, you might uncover ... Read More

How to list directory tree structure in python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 11-Sep-2023 17:14:58

4K+ Views

While working with file systems and directories in Python, understanding the directories’ structure and their contents is essential for efficient file management and organization. Python makes available various methods and libraries that allow you to list the directory tree structure, including all subdirectories and files, in a comprehensive and exhaustive ... Read More

Advertisements