Server Side Programming Articles

Page 319 of 2109

Python Program to check if String contains only Defined Characters using Regex

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 561 Views

When it is required to check if a given string contains only specific characters using regular expression, a regular expression pattern is defined, and the string is subjected to follow this pattern. Understanding the Regex Pattern The pattern ^[Python]+$ breaks down as follows: ^ − Start of string [Python] − Character set containing P, y, t, h, o, n + − One or more occurrences $ − End of string Example Below is a demonstration of the same − import re def check_string(my_string, regex_pattern): if re.search(regex_pattern, ...

Read More

Python Program to accept string ending with alphanumeric character

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 700 Views

When it is required to check if a string ends with an alphanumeric character or not, regular expressions can be used. An alphanumeric character is any letter (a-z, A-Z) or digit (0-9). This tutorial demonstrates how to create a function that validates whether a string's last character is alphanumeric. Using Regular Expressions The re.search() method can match patterns at the end of a string using the $ anchor ? import re regex_expression = '[a-zA-Z0-9]$' def check_string(my_string): if re.search(regex_expression, my_string): print("The string ends ...

Read More

Python - Check whether a string starts and ends with the same character or not

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 2K+ Views

When it is required to check if a string begins and ends with the same character or not, there are multiple approaches. You can use regular expressions, simple string indexing, or built-in string methods. Using Regular Expression A method can be defined that uses the 'search' function to see if a string begins and ends with a specific character ? import re regex_expression = r'^[a-z]$|^([a-z]).*\1$' def check_string(my_string): if(re.search(regex_expression, my_string)): print("The given string starts and ends with the same character") ...

Read More

Python regex to find sequences of one upper case letter followed by lower case letters

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 960 Views

When working with text processing, you often need to find sequences of one uppercase letter followed by lowercase letters. Python's regex module provides an efficient way to match such patterns using regular expressions. Understanding the Pattern The regex pattern [A-Z][a-z]+ matches one uppercase letter followed by one or more lowercase letters. Let's break it down ? [A-Z] − matches exactly one uppercase letter [a-z]+ − matches one or more lowercase letters $ − ensures the pattern matches to the end of string Basic Pattern Matching Here's a simple function to check if a ...

Read More

Python - Remove all characters except letters and numbers

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 1K+ Views

When it is required to remove all characters except for letters and numbers, regular expressions are used. A regular expression is defined, and the string is subjected to follow this expression. Using Regular Expressions with re.sub() The re.sub() function replaces all non-alphanumeric characters with an empty string ? import re my_string = "python123:, .@! abc" print("The string is:") print(my_string) result = re.sub('[\W_]+', '', my_string) print("The expected string is:") print(result) The output of the above code is ? The string is: python123:, .@! abc The expected string is: python123abc ...

Read More

Python - How to group DataFrame rows into list in Pandas?

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 407 Views

When working with Pandas DataFrames, you may need to group rows and collect values into lists. This is commonly done using the groupby() method combined with apply(list). Basic Grouping with apply(list) The simplest approach uses groupby() with apply(list) to collect values ? import pandas as pd # Create DataFrame dataFrame = pd.DataFrame({ "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] }) print("Original DataFrame:") print(dataFrame) # Group by Car and collect Units into lists grouped = dataFrame.groupby('Car')['Units'].apply(list) print("Grouped DataFrame:") print(grouped) ...

Read More

Python Program to Group Strings by K length Using Suffix

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 304 Views

When it is required to group strings by K length using a suffix, a simple iteration and the 'try' and 'except' blocks are used. This technique extracts the last K characters from each string and groups strings with the same suffix together. Example Below is a demonstration of the same − my_list = ['peek', "leak", 'creek', "weak", "good", 'week', "wood", "sneek"] print("The list is :") print(my_list) K = 3 print("The value of K is") print(K) my_result = {} for element in my_list: suff = element[-K : ] ...

Read More

Python – Replacing by Greatest Neighbors in a List

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 250 Views

When it is required to replace elements of a list by their greatest neighbors, a simple iteration along with the 'if' and 'else' condition is used. Each element (except the first and last) gets replaced by whichever neighbor has the greater value. Syntax The general approach involves iterating through the list and comparing adjacent elements − for index in range(1, len(list) - 1): list[index] = list[index - 1] if list[index - 1] > list[index + 1] else list[index + 1] Example Below is a demonstration of replacing elements by ...

Read More

Python – Filter dictionaries with ordered values

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 300 Views

When filtering dictionaries to find those with values in ascending order, we can use the sorted() method along with list comprehension to compare the original values with their sorted version. Syntax [dict for dict in list_of_dicts if sorted(dict.values()) == list(dict.values())] Example Below is a demonstration of filtering dictionaries with ordered values − my_list = [{'python': 2, 'is': 8, 'fun': 10}, {'python': 1, 'for': 10, 'coding': 9}, {'cool': 3, 'python': 4}] ...

Read More

Python Pandas – Filter DataFrame between two dates

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 2K+ Views

To filter DataFrame between two dates, use the dataframe.loc method. This allows you to select rows based on date conditions using boolean indexing. Creating a DataFrame with Date Records At first, import the required library and create a DataFrame with date columns − import pandas as pd # Dictionary of lists with date records d = {'Car': ['BMW', 'Lexus', 'Audi', 'Mercedes', 'Jaguar', 'Bentley'], 'Date_of_Purchase': ['2021-07-10', '2021-08-12', '2021-06-17', ...

Read More
Showing 3181–3190 of 21,090 articles
« Prev 1 317 318 319 320 321 2109 Next »
Advertisements