SaiKrishna Tavva

SaiKrishna Tavva

80 Articles Published

Articles by SaiKrishna Tavva

Page 5 of 8

How to strip spaces/tabs/newlines using Python regular expression?

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 24-Mar-2026 1K+ Views

Python regular expressions (regex) provide powerful methods to strip whitespace characters including spaces, tabs, and newlines from strings. The re module offers several approaches to handle different whitespace scenarios. This article explains how to strip various types of whitespace using regular expressions, covering the following methods ? Stripping All Whitespace Using re.sub() Stripping Leading/Trailing Whitespace Splitting on Whitespace Characters Stripping Specific Whitespace Types Stripping All Whitespace Using re.sub() The re.sub() function replaces all occurrences of a pattern with ...

Read More

How to split on successions of newline characters using Python regular expression?

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 24-Mar-2026 504 Views

Python's built-in splitlines() method and the split() method with as a delimiter are sufficient to split strings based on newline characters. This article will explore different approaches to splitting strings on sequences of newline characters using Python's regular expressions. Splitting on One or More Newlines The Python re.split() function uses a regular expression to split a string. We'll use the pattern +, which means one or more newlines. The re.split() will find where these newlines are, split the string there, and return a list of the resulting pieces. Example The following code splits the text ...

Read More

How to write a Python regular expression to get numbers except decimal?

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 24-Mar-2026 408 Views

Python's regular expressions (regex) allow us to search, extract, and manipulate patterns in strings. When we want to extract only whole numbers from a string (excluding decimals), we can use re.findall() method with a specific regex pattern. The regex pattern \b\d+\b is designed to match integer patterns specifically. This pattern matches numbers like 10, 245, 89 but not 12.34 or 0.56 (decimals). \b − Word boundary to ensure we match standalone numbers \d+ − One or more digits (0−9) \b − Ending word boundary Basic Extraction ...

Read More

How you can get a true/false from a Python regular expressions?

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 24-Mar-2026 5K+ Views

The re module provides tools while working with Python's regular expression (regex), which allows us to search for any specific pattern within strings. Using the re module, we can get True/False results from regular expressions by checking if pattern matching functions return a match object or None. Three main functions help achieve this ? re.match(): Checks if the pattern matches the beginning of the string. re.search(): ...

Read More

How to merge two Python dictionaries in a single expression?

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 24-Mar-2026 388 Views

In Python, a Dictionary is an unordered and mutable data structure which stores data as key-value pairs. In some cases, we may need to merge two dictionaries into a single dictionary using a single expression. Python provides several methods to accomplish this task efficiently. Using | Merge Operator (Python 3.9+) The | merge operator is the most modern and readable way to merge dictionaries in a single expression. It returns a new dictionary containing all key-value pairs from both dictionaries. If both dictionaries have the same key, the value from the right-side dictionary takes precedence ? ...

Read More

How to search and replace text in a file using Python?

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 24-Mar-2026 17K+ Views

File manipulation is a fundamental aspect of programming, especially when dealing with data processing and management. Python offers powerful tools for handling files and text efficiently. A common task is searching for specific text patterns within a file and replacing them with desired content. This article explores several practical methods for searching and replacing text in a file using Python. Basic Text Replacement Let's start with a simple example of searching for a specific word in a file and replacing it with another word. We'll search for the word "old" and replace it with "new" ? ...

Read More

What are the modes a file can be opened using Python?

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 24-Mar-2026 8K+ Views

When working with files in Python, it's crucial to understand the different modes in which files can be opened. Each mode defines specific operations you can perform − whether reading, writing, appending, or handling binary data. Following are the common file modes in Python. Read Mode: 'r' Write Mode: 'w' Binary Mode: 'b' Append Mode: 'a' Read Mode: 'r' The default mode for opening files in Python is read mode ('r'). This allows you to read the contents of a file without modifying ...

Read More

How to ignore hidden files using os.listdir() in Python?

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 24-Mar-2026 7K+ Views

When working with directories in Python, hidden files can clutter your file listings. Hidden files start with a dot (.) on Unix-like operating systems and can be easily filtered out using various Python techniques. In this article, we will explore different methods to ignore hidden files using the os module and pathlib. Using List Comprehension with os.listdir() The os.listdir() method includes hidden files (those starting with a dot) in its results. To filter out hidden files and list only visible files, we can use list comprehension with a condition ? import os # List ...

Read More

How to get specific nodes in xml file in Python?

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 24-Mar-2026 10K+ Views

XML is abbreviated as Extensible Markup Language which is a format used to represent structured data. It's useful when exchanging information between systems. In Python, the xml.etree.ElementTree module helps us read and work with XML data. In this article, we will explore how to extract specific nodes from an XML file using this library. Introduction to XML and ElementTree When working with XML files in Python, the xml.etree.ElementTree module is used for parsing and navigating XML structures. It reads an XML document and builds a tree of elements, allowing us to easily access and manipulate individual parts of ...

Read More

How to copy certain files from one folder to another using Python?

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 24-Mar-2026 4K+ Views

When working with files in Python, we often need to copy only certain types of files from one folder to another. Python provides several built-in modules like os, shutil, and pathlib to make this task simple and flexible. Using shutil and os Modules The shutil module performs high-level file operations like copying, moving, and archiving, while the os module handles operating system interactions for file and directory manipulation. Copying Files by Extension Here's how to copy all .txt files from a source folder to a destination folder ? import os import shutil # ...

Read More
Showing 41–50 of 80 articles
« Prev 1 3 4 5 6 7 8 Next »
Advertisements