Rajendra Dharmkar has Published 181 Articles

How to check whether a string ends with one from a list of suffixes in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 02-Jun-2025 17:30:08

915 Views

A suffix is a group of letters added at the end of a word. In Python, we can check if a string ends with any one of multiple suffixes using the endswith() method. It takes a tuple of suffixes as an argument and returns True if the string ends ... Read More

How to handle invalid arguments with argparse in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 02-Jun-2025 17:29:49

2K+ Views

Argparse is a Python module that helps you create easy-to-use command-line interfaces. When building these interfaces, it is important to handle invalid arguments properly to give clear feedback to users and prevent your program from crashing unexpectedly. There are several ways to handle invalid arguments in argparse. You can ... Read More

What is the difference between re.findall() and re.finditer() methods available in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 26-May-2025 17:01:49

1K+ Views

This article gives a guide on the difference between re.findall() and re.finditer() methods available in Python. The re module of Python helps in working with regular expressions. Both the re.findall() and re.finditer() methods are used to search for patterns, but they behave differently. Let us see the differences with simple explanations ... Read More

How do I find the location of my Python site-packages directory?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 15-May-2025 17:54:22

3K+ Views

The site-packages directory in python, is the place where third-party libraries and packages are installed. Knowning the site-packeges location is useful for debugging, verifying installations or inspecting package contents. This directory may vary depending on our operating system, Python version or based on the virtual environment we are using. In ... Read More

How to raise Python exception from a C extension?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 15-May-2025 14:00:10

217 Views

When writing a C extension for Python, you might need to raise an exception if something goes wrong in your C code. Python provides a special C API that helps you to raise errors in a way that works just like regular Python exceptions.This is helpful because even if your ... Read More

What are the best practices to organize Python modules?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 28-Apr-2025 12:30:19

566 Views

Organizing Python modules efficiently is important to maintain scalability and collaboration. Following best practices can make your project easier to understand and use. In this article, we will discuss about an structure approach to organize Python modules with a sample project and some of the best practices to organize. Sample ... Read More

How can I apply an offset on the current time in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 04-Nov-2023 01:13:51

4K+ Views

Whenever you want to add or subtract(apply an offset) 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 has the following function signature − datetime.timedelta([days[, seconds[, microseconds[, milliseconds[, minutes[, hours[, weeks]]]]]]]) ... Read More

How to convert unix timestamp string to readable date in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 02-Nov-2023 13:13:20

5K+ Views

You can use the fromtimestamp() function from the datetime module to get a date from a UNIX timestamp. This function takes the timestamp as input and returns the datetime object corresponding to the timestamp.  Exmaple import datetime timestamp = datetime.datetime.fromtimestamp(1500000000) print(timestamp.strftime('%Y-%m-%d %H:%M:%S'))OutputThis will give the output −2017-07-14 08:10:00Read More

How can we do date and time math in Python?

Rajendra Dharmkar

Rajendra Dharmkar

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

4K+ 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

3K+ 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

1 2 3 4 5 ... 19 Next
Advertisements