Python Articles

Page 172 of 855

Python program to count the number of spaces in string

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

In this article, we will learn how to count the number of spaces in a string using various Python methods. Counting spaces is useful for text analysis, validation, and formatting tasks. Methods Used The following are the various methods to accomplish this task − Using for loop with indexing Using count() function Using isspace() function Using Counter() function Using countOf() function from operator module Method 1: Using For Loop with Indexing This method iterates through each character using index positions and counts spaces manually ? def countSpaces(inputString): ...

Read More

Python Program to Count of Words with specific letter

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

In this article, we will learn how to count words containing a specific letter in a string using Python. We'll explore four different approaches to solve this problem efficiently. Problem Statement Given an input string and a character, we need to count how many words contain that specific character. Example input_string = 'hello tutorialspoint python codes' input_char = "p" # Expected output: 2 (words: tutorialspoint, python) Method 1: Using List Comprehension with split() List comprehension provides a concise way to filter words containing the target character − # Input string ...

Read More

Python Program to Alternate list elements as key-value pairs

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

In this article, we will learn how to get alternate list elements as key-value pairs in Python. This technique is useful when you need to transform a list into a dictionary by pairing elements at alternate positions. Assume we have an input list containing integers. We need to create key-value pairs where each element is paired with the element that appears two positions ahead ? Methods Used The following are the various methods used to accomplish this task ? Using for loop Using dictionary comprehension and list slicing ...

Read More

How to Common keys in list and dictionary using Python

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

In this article, we will learn how to find common keys between a list and dictionary in Python. This is useful when you need to filter dictionary keys based on a list of allowed values. Methods Used The following are the various methods to accomplish this task − Using the 'in' operator and List Comprehension Using set() and intersection() functions Using keys() function & in operator Using the Counter() function Example Setup Assume we have taken an input dictionary and list. We will find the common elements between the input list and keys ...

Read More

Find the difference of the sum of list elements that are missing from Matrix and vice versa in python

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

In this article, we will learn how to find the difference of the sum of list elements that are missing from Matrix and vice versa. This problem involves comparing elements between a 2D matrix and a 1D list to calculate the absolute difference of their unique elements' sums. Problem Overview Given a matrix and a list, we need to − Find elements in the list that are not in the matrix Find elements in the matrix that are not in the list Calculate the absolute difference between their sums Example Let's understand with ...

Read More

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 791 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
Showing 1711–1720 of 8,549 articles
« Prev 1 170 171 172 173 174 855 Next »
Advertisements