Server Side Programming Articles

Page 152 of 2109

Case-insensitive string replacement using Python Program

Vikram Chiluka
Vikram Chiluka
Updated on 26-Mar-2026 5K+ Views

Case-insensitive string replacement is a common text processing task where you need to replace substrings regardless of their letter case. Python provides several approaches using regular expressions, string methods, and list comprehensions. Using re.IGNORECASE with re.compile() The regex module provides re.IGNORECASE flag to ignore case differences during pattern matching ? import re # input string input_string = "Hello TutorialsPOINT Python" print("Input String:", input_string) # substring to be replaced and replacement substring = "tutorialspoint" replace_string = "Java" # compile regex with IGNORECASE flag compiled_pattern = re.compile(re.escape(substring), re.IGNORECASE) # perform case-insensitive replacement result = ...

Read More

Interchange Diagonals of Matrix using Python

Vikram Chiluka
Vikram Chiluka
Updated on 26-Mar-2026 802 Views

In this article, we will learn how to interchange the main diagonal and anti-diagonal elements of a square matrix using Python. This operation swaps elements at position (i, i) with elements at position (i, n-1-i) for each row. We will explore two different approaches to accomplish this task using a square NxN matrix. Methods Used The following methods will be demonstrated − Using Nested For Loops with Temporary Variable Using Tuple Swapping Algorithm Following are the steps to interchange matrix diagonals − Create a function to print the matrix in ...

Read More

How to perform accurate Decimal Calculations using Python?

Vikram Chiluka
Vikram Chiluka
Updated on 26-Mar-2026 2K+ Views

Floating-point arithmetic in Python can introduce precision errors when performing decimal calculations. This article explores two methods to achieve accurate decimal calculations: using the decimal module and the math.fsum() function. The Problem with Floating-Point Arithmetic Standard floating-point numbers cannot accurately represent all decimal values due to IEEE 754 standard limitations. This leads to unexpected calculation errors ? x = 4.2 y = 3.1 # printing the sum of both variables print("x + y =", x + y) # checking if the sum equals 7.3 print((x + y) == 7.3) x + ...

Read More

How to Manipulating Pathnames using Python?

Vikram Chiluka
Vikram Chiluka
Updated on 26-Mar-2026 2K+ Views

In this article, we will learn how to manipulate pathnames using Python's os.path module. Python provides several built-in functions to work with file paths, making it easy to extract components, join paths, and handle different operating systems. We will explore the following pathname manipulation techniques ? Getting the main filename from the file path Getting the directory name from the file path Joining path components together Expanding the user's home directory Splitting the file extension from the file path Getting the Main Filename The os.path.basename() function returns the last component (filename) from ...

Read More

How to Iterate over Tuples in Dictionary using Python

Vikram Chiluka
Vikram Chiluka
Updated on 26-Mar-2026 3K+ Views

In this article, we will learn how to iterate over tuples stored as values in a Python dictionary. Dictionaries can contain tuples as values, and we often need to access and process these tuple elements efficiently. Methods Used The following are the various methods used to accomplish this task ? Using Direct Indexing Using dictionary.values() Method Using dictionary.items() Method Tuples are immutable, ordered collections in Python. Unlike lists, tuples have a fixed length and cannot be modified after creation, making them ideal for storing related data that shouldn't change. Method 1: Using Direct ...

Read More

How to get the file name from the file path in Python?

Vikram Chiluka
Vikram Chiluka
Updated on 26-Mar-2026 9K+ Views

When working with file paths in Python, you often need to extract just the filename from a complete file path. Python provides several methods to accomplish this task using different modules. Method 1: Using os.path.basename() The most common and straightforward approach is using the os.path.basename() function, which returns the final component of a file path ? import os # Input file path file_path = 'C:/Users/Documents/tutorial.pdf' # Extract filename using os.path.basename() filename = os.path.basename(file_path) print("The filename is:", filename) The filename is: tutorial.pdf Method 2: Using os.path.split() The split() function ...

Read More

How to Get a Negation of a Boolean in Python?

Vikram Chiluka
Vikram Chiluka
Updated on 26-Mar-2026 7K+ Views

In this article, we will learn how to get a negation of a Boolean in Python. In Python, the Boolean datatype is a built-in data type that represents True or False values. For example, 520 is False. Boolean negation means converting True to False and False to True. The following are the various methods to accomplish this task ? Using the "not" operator Using the "~" operator Using operator module Using arithmetic subtraction from 1 Using NumPy functions Method 1: Using the "not" Operator The not operator is the most common and pythonic ...

Read More

How to generate IP addresses from a CIDR address using Python?

Vikram Chiluka
Vikram Chiluka
Updated on 26-Mar-2026 5K+ Views

In this article, we will learn how to generate IP addresses from a CIDR (Classless Inter-Domain Routing) address using Python's built-in ipaddress module. CIDR notation represents a network address and its subnet mask, allowing us to define IP address ranges efficiently. What is CIDR Notation? CIDR notation combines an IP address with a prefix length (e.g., 192.168.1.0/24). The number after the slash indicates how many bits are used for the network portion, determining the range of available host addresses. Using IPv4Network The ipaddress.ip_network() function creates a network object from CIDR notation, allowing us to iterate through ...

Read More

How to find Profit or loss using Python when CP of N items is equal to SP of M

Vikram Chiluka
Vikram Chiluka
Updated on 26-Mar-2026 668 Views

In this article, we will learn a Python program to find the profit or loss when the cost price (CP) of N items is equal to the selling price (SP) of M items. When CP of N items equals SP of M items, we can calculate profit or loss percentage using a mathematical relationship between these quantities. Understanding the Problem Given that Cost Price of N items = Selling Price of M items, we need to determine whether there's a profit or loss and calculate the percentage. What is Cost Price (CP)? Cost price is ...

Read More

How to Determine Last Friday's Date using Python?

Vikram Chiluka
Vikram Chiluka
Updated on 26-Mar-2026 4K+ Views

In this article, we will learn how to determine last Friday's date using Python. This is useful for scheduling, reporting, and date-based calculations in business applications. Methods Used The following are the various methods to accomplish this task − Using datetime module Using dateutil package Method 1: Using Datetime Module This approach uses Python's built-in datetime module to calculate the previous Friday by determining the current weekday and counting backwards. Algorithm Following are the steps to perform this task − Import datetime and timedelta from datetime module Create a ...

Read More
Showing 1511–1520 of 21,090 articles
« Prev 1 150 151 152 153 154 2109 Next »
Advertisements