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
Articles on Trending Technologies
Technical articles with clear explanations and examples
How to match non-word characters in Python using Regular Expression?
A Non-word character is any type of character that is not a letter, digit, or underscore, i.e., anything other than [a-z, A-Z, 0-9_]. Examples of non-word characters are symbols, punctuation marks, spaces, tabs, and other special characters. In Python, Regular Expressions (RegEx) are used to match the patterns in the given input strings. The re module in Python provides different methods to use the regular expressions. This module helps the developers to perform search operations, validations, filtering and much more, based on string patterns. To match a non-word character in Python, we can use the special ...
Read MoreHow to match a word in python using Regular Expression?
In Python, Regular Expressions (RegEx) are used to match patterns in the given input strings. The re module in Python provides different methods to use the regular expressions. This module helps the developers to perform search operations, validations, filtering, and much more based on string patterns. In this article, we will explore different ways to match a word in Python using the re module. Match a Specific Word using re.search() In Python, the re.search() method is used to search for a pattern within a given string. If the pattern is found, then it returns a match object ...
Read MoreHow to merge two Python dictionaries in a single expression?
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 MoreHow to change any data type into a string in Python?
In Python, converting different data types into strings is a common requirement when we want to display or store information in text format. The built-in str() function is the primary method for this conversion, accepting any Python object and returning its string representation. Syntax str(object) Where object is any valid Python object, such as int, float, bool, list, etc. Converting Integer to String An integer can be converted into a string using the str() function. This conversion is useful when concatenating numbers with strings ? num = 42 string_num = str(num) ...
Read MoreWhat are different data conversion methods in Python?
Data conversion in Python refers to changing the data type of a value to another data type. Python supports two different types of data conversion: Implicit conversion and Explicit conversion. Implicit Conversion is performed automatically by the Python interpreter. Explicit Conversion is manually done by the programmer using built-in functions. Implicit Conversion Python automatically converts one data type to another to avoid data loss during operations. The smaller data type is converted to a higher data type ? Example The following example shows how Python ...
Read MoreHow to print a string two times with single statement in Python?
When developing Python applications, you may need to repeat or duplicate a string for display or formatting purposes. Python provides several ways to print a string twice with a single statement. There are different approaches to print a string twice with a single statement in Python: Using Multiplication Operator Using the Concatenation Operator Using the format() Method Using f-string Using join() Method with a List Using List Unpacking Using ...
Read MoreHow to divide a string by line break or period with Python regular expressions?
When working with text processing in Python, you often need to split strings by multiple delimiters like periods and line breaks. Python's regular expressions module re provides powerful pattern matching for this purpose. Using re.findall() to Split by Period and Line Break The re.findall() function extracts all substrings that match a given pattern. Here's how to split a string by periods and line breaks − import re s = """Hi. It's nice meeting you. My name is Jason.""" result = re.findall(r'[^\s\.][^\.]+', s) print(result) ['Hi', "It's nice meeting you", 'My name is ...
Read MoreHow can I match the start and end in Python\'s regex?
In Python, the re module is used to work with regular expressions. In some cases, we need to check whether a string starts or ends with a specific pattern, and then we need to use special regex characters called anchors. Following are the anchors used in regular expressions − ^ − This anchor matches the beginning of a string. $ − This anchor matches the end of a string. Python Regex Methods The following methods from the re module are commonly used to apply start and end ...
Read MoreHow do I do a case-insensitive string comparison in Python?
In Python, string comparisons are case-sensitive by default. For example, when we consider the strings "Hello" and "hello" then they are considered as two different strings because of the difference in their letter casing. Case-insensitive String Comparison in Python In some tasks, such as searching user input, comparing filenames, or processing natural language, it's important to compare strings without considering case differences. To perform string comparison in Python, we have several built-in methods such as lower(), upper(), and casefold(), which help us to normalize the casing of strings before comparing them. ...
Read MoreHow to search and replace text in a file using Python?
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