Found 10476 Articles for Python

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

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:00

How to subtract Python timedelta from date in Python?

Rajendra Dharmkar
Updated on 28-Aug-2025 12:15:29

11K+ Views

The Python datetime module provides various ways for manipulating dates and times. One of its key features is the timedelta object, which represents duration and also the difference between two dates or times. Subtracting a specific amount of time from a date can be done using timedelta. For example, if we want to find the date a day before today, then we create a timedelta object with days=1 and then subtract it from the current date. Subtracting One Day from Today's Date Using timedelta The basic use case is subtracting a single day from today's date. This can be done ... Read More

How to convert Python date format to 10-digit date format for mysql?

Pranav Indukuri
Updated on 28-Aug-2025 11:40:05

2K+ Views

While working with databases like MySQL, it’s necessary to store dates in a numeric format, especially for timestamps. MySQL commonly uses Unix timestamps, which are 10-digit numbers representing the number of seconds since January 1, 1970 (known as the epoch). The following are the different methods from the time and datetime modules to convert Python date formats into a 10-digit format (Unix timestamp) suitable for use with MySQL. Using mktime() Method Using strptime() + mktime() Combination Using timestamp() Method Using mktime() Method The mktime() method from ... Read More

How to convert date to datetime in Python?

Pranav Indukuri
Updated on 28-Aug-2025 12:23:06

41K+ Views

In this article, we will discuss how to convert a date to a datetime object in Python. We use the combine() method from the Date & Time module to combine a date object and a time object into a single datetime object. While the date object represents only the calendar date (year, month, day), sometimes we need the full datetime object that includes time (hour, minute, second) as well. Following are the several ways to achieve this. The syntax of the combine() method is as follows. datetime.combine(date, time) Converting a date object to a datetime Object Using the combine() Method The ... Read More

Which one is more accurate in between time.clock() vs. time.time()?

SaiKrishna Tavva
Updated on 15-May-2025 19:38:13

589 Views

Two commonly used functions from the Python time module are time.time() and time.clock(). Each function provides a different purpose and returns different values depending on the platform (Windows vs. Unix). In Python 3.8, time.clock() was removed, so time.perf_counter() or time.process_time() are generally preferred over the older time.clock() for specific CPU time measurements. The time.clock() was designed for measuring process CPU time, while time.time() measures wall-clock time. The time.time() is more accurate for measuring overall elapsed time ( the duration of time that has passed between two specific points in time). Measuring Elapsed Time with time.time() The time.time() function returns the number of ... Read More

How to return the current CPU time in Python?

Pranav Indukuri
Updated on 28-Aug-2025 12:14:05

5K+ Views

In this article, we retrieve the current CPU time in Python using the time() method,  which is imported from the Python time module. The time module in Python provides various methods and functions related to time. Here we use the time.The time() method to get the current CPU time in seconds. The time is calculated since the epoch. This method returns a float value that represents the seconds since the epoch. Epoch is the starting point of time and is platform-dependent. The epoch is January 1, 1970, 00:00:00 (UTC) on Windows and most Unix systems, and leap seconds are not included in the ... Read More

What are negated character classes that are used in Python regular expressions?

SaiKrishna Tavva
Updated on 15-May-2025 19:40:32

1K+ Views

 While working with Python regex,  if we want to match everything except certain characters, then we can use negated character classes by placing a caret (^) as the first character inside square brackets. The pattern [^abdfgh] will match any character not in that set. What is a Negated Character Class? A character class like [abc] matches any single character except 'a', 'b', or 'c'. But if we use a ^ symbol at the beginning, like [abc], it will match any character except 'a', 'b', or 'c'. This allows us to eliminate certain characters from the match quickly. The position of ... Read More

What are metacharacters inside character classes used in Python regular expression?

Rajendra Dharmkar
Updated on 28-Aug-2025 11:42:12

468 Views

Python's regular expressions provide various ways to search and manipulate strings. They are used to define search patterns that can be matched in text data. These patterns are defined using a set of characters known as metacharacters, which carry special meaning in regex. Their behaviour can change when used inside character classes (also known as character sets or square brackets '[]'). Understanding how metacharacters are interpreted within character classes is crucial for writing accurate and effective regular expressions. Understanding Character Classes In Python regex, character classes are denoted by square brackets '[ ]', which define a set of characters or ... Read More

What are repeating character classes used in Python regular expression?

Rajendra Dharmkar
Updated on 30-Jul-2019 22:30:21

553 Views

A character class followed by operators like '?', '*' or '+' are called repeating character classes.If you repeat a character class by using the '?', '*' or '+' operators, you will repeat the entire character class, and not just the character that it matched. The regex '[0-9]+' can match '579' as well as '333'. If you want to repeat the matched character, rather than the class, you will need to use backreferences. '([0- 9])\1+' will match '333' but not “579”. When applied to the string “922226”, it will match '2222' in the middle of this string. If you do not ... Read More

What are character class operations in Python?

Rajendra Dharmkar
Updated on 28-Aug-2025 11:09:56

2K+ Views

Character class operations in Python's regular expressions allow us to define set of characters we want to match. Instead of searching for one specific character, we can search for any character within that set. A character class in regex is written using square brackets []. It defines a group of characters where any character from the group can match a part of the string. Commonly Used Character Classes in Python (re module) Regular expressions use both normal and special characters. Normal characters like 'A', 'a', or '0' match themselves. So, "last" (a sequence of characters) matches the string 'last'. Some characters, ... Read More

Advertisements