Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Programming Articles
Page 652 of 2547
How to capture an exception raised by a Python Regular expression?
This article provides a guide on how to capture exceptions raised by regular expressions in Python. We will explore simple example programs that demonstrate proper error handling techniques. Regular expressions are useful for matching patterns in text, but they may result in errors like incorrect syntax or mismatches. Python uses exception handling (try-except blocks) to handle these errors gracefully and prevent program crashes. For example, if the given pattern is "[a-z", this is an incorrect pattern that will raise an error because the closing bracket "]" is missing from the character set. Python Regex Exception Type ...
Read MoreHow to compare regular expressions in Perl and Python?
This article compares regular expressions in Perl and Python, showing syntax differences and practical examples. Regular expressions are patterns used to match and manipulate strings in both languages. The key difference is syntax: Perl uses $text =~ /pattern/ while Python uses re.search('pattern', text). Let's explore practical examples comparing both approaches. Check if a String Contains a Word Both languages can search for specific words in text, but use different syntax. Python Example Python uses the re.search() function to find patterns in strings ? import re text = "Hello Tutorialspoint" match = re.search(r"Tutorial", ...
Read MoreWhat is difference between \'.\' , \'?\' and \'*\' in Python regular expression?
This article will discuss the difference between dot '.', question mark '?', and asterisk '*' in Python regular expressions. In regular expressions, '.' means any one character except the newline, '*' means zero or more occurrences of the preceding character or group, and '?' means zero or one occurrence of the preceding character or group. The question mark can also make quantifiers non-greedy in some contexts. Let's understand these symbols using practical examples ? Usage of Dot (.) The dot '.' matches any single character except a newline ? import re # Define ...
Read MoreWhat is Raw String Notation in Python regular expression?
Raw string notation in Python regular expressions helps avoid backslash conflicts by treating backslashes as literal characters rather than escape sequences. This is especially important when writing regex patterns that contain backslashes. The Problem with Regular Strings In regular Python strings, backslashes have special meaning for escape sequences like (newline) and \t (tab). This can interfere with regex patterns that need literal backslashes ? import re # Without raw string - needs double backslashes pattern = "\d+" text = "There are 123 items" matches = re.findall(pattern, text) print("Without raw string:", matches) ...
Read MoreHow to convert a Python date string to a date object?
In this article, we convert a date string to a date object. We will use the strptime() method to convert a date string to a date object, and we will also use the dateutil module for flexible date parsing. A date string is a date written in text form, like "09 May 2025" or "2025-05-09". To work with dates in Python, we need to convert these strings into date objects. We can achieve this using Python's datetime and dateutil modules. Using the strptime() Function The strptime() method takes a date string as input and converts it into ...
Read MoreHow to get formatted date and time in Python?
This article will discuss how to format date and time in Python. Python makes it easy to work with date and time with the help of the datetime module. You can format dates and times in different formats as per your needs. Datetime Module of Python The datetime module in Python offers methods for working with date and time values. To use this module, we must first import it using the following import keyword − import datetime The strftime() Function The strftime() function returns a formatted date and time string. It accepts a ...
Read MoreHow to get current date and time in Python?
In this article, we will show you how you can get the current time and date in Python with the help of different functions. Python provides two main modules for working with date and time: the datetime module and the time module. Using the Datetime Module Date and time are not data types in Python, but you can work with them using the datetime module. The datetime module is included in Python, so there is no need to install it separately. Using the now() Method The now() method returns the current local date and time ? ...
Read MoreHow to print complete TimeTuple in Python?
In Python, a TimeTuple is a named tuple containing 9 elements that represent different components of date and time. The timetuple() method converts datetime objects into this structured format, making it easy to access individual time components like year, month, day, hour, minute, and second. The time tuple contains 9 integers: year, month, day, hour, minute, second, weekday, year day, and daylight saving time flag. Here are different ways to print a complete time tuple ? Using datetime.date.today() ...
Read MoreHow to get timer ticks at a given time in Python?
In this article, we will show you how to get timer ticks at a given time using different functions in Python. Timer ticks represent time measurements that help track execution time, current time, and performance metrics. The time module in Python provides several functions to work with time measurements. Here are the main approaches to solve this problem ? Using time.time() Function Using time.perf_counter() Function Using time.process_time() Function Using time.time() Function The time.time() function returns the current time in seconds since the Unix epoch (January 1, 1970). It's useful for getting timestamps and measuring ...
Read MoreWhat is a Tick in python?
In Python, a tick is a floating-point number representing time in seconds since January 1, 1970, 00:00:00 UTC (also known as the Unix epoch). This timestamp format is widely used for time calculations, logging, and scheduling tasks in Python programs. The time.time() function from Python's time module returns the current tick value, making it essential for measuring execution time and working with timestamps. Getting the Current Tick Value The time.time() function retrieves the current tick value. This value continuously increases as time progresses and represents UTC time ? Example import time # Get ...
Read More