A non-whitespace character is any character that is not a space, tab i.e., \t, newline i.e., or carriage return \r. Examples of non-whitespace characters include letters, digits, punctuation and special symbols. In Python, Regular Expressions (RegEx) are used to match patterns in input strings. The re module in Python provides different methods to use regular expressions. This module helps developers perform search operations, validations, filtering and much more based on string patterns. To match a non-whitespace character in Python we can use the special character class \S inside a raw string with any re module method. In ... Read More
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 More
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 More
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
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 More
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 More
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 More
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 More
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 More
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 More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance