Programming Articles

Page 12 of 2547

How to scan a string for specific characters in Python?

Tarun Chandra
Tarun Chandra
Updated on 24-Mar-2026 4K+ Views

Scanning a string for specific characters is a common task in Python programming. There are several approaches to check if certain characters exist in a string, each suited for different scenarios. Using the 'in' Operator The simplest method is using the in keyword to check if a single character exists in a string. It returns True if the character is found, False otherwise ? text = "Welcome to Tutorialspoint" char = 'T' print(f"String: {text}") print(f"Character to find: '{char}'") print(f"Character found: {char in text}") String: Welcome to Tutorialspoint Character to find: 'T' Character ...

Read More

How to find the longest common substring from more than two strings in Python?

Yaswanth Varma
Yaswanth Varma
Updated on 24-Mar-2026 2K+ Views

The substrings are sequences of characters that appear within a string. When working with multiple strings, finding the longest common substring helps identify shared patterns across all strings. There are various ways to find the longest common substring from more than two strings. One of the most common approaches is using dynamic programming. Let's explore this approach in detail. Using Dynamic Programming In this approach, we use dynamic programming (DP) to compare strings pairwise. We start with the first two strings and find their longest common substring using a DP table. The algorithm works as follows: ...

Read More

How to check if a string can be converted to float in Python?

Tarun Chandra
Tarun Chandra
Updated on 24-Mar-2026 2K+ Views

When working with user input or data processing, you often need to check if a string can be converted to a float before performing the conversion. Python provides several approaches to validate float conversion safely. Using float() with Exception Handling The most reliable approach uses the float() function with try-except handling. This method attempts the conversion and catches any ValueError that occurs ? def is_float(s): try: float(s) return True except ValueError: ...

Read More

How to find longest repetitive sequence in a string in Python?

Yaswanth Varma
Yaswanth Varma
Updated on 24-Mar-2026 3K+ Views

Strings are essential data types used in many real-world problems that involve analyzing and manipulating text data. In this article, we are going to learn about finding the longest repetitive sequence in a string. The repetitive sequence refers to a substring that appears more than once in the given string. Python provides several built-in features to accomplish this task efficiently. Using Suffix Array and LCP A suffix array is used to store all the suffixes of the given string in lexicographic order. In this approach, we create a list of all suffixes of the string, sort ...

Read More

How to remove all special characters, punctuation and spaces from a string in Python?

Yaswanth Varma
Yaswanth Varma
Updated on 24-Mar-2026 7K+ Views

Cleaning strings by removing unwanted elements such as special characters, punctuation, and spaces is essential in text processing. These unwanted characters can interfere with data analysis tasks and cause unexpected results. In this article, we'll explore three effective methods to remove all special characters, punctuation and spaces from strings in Python. Using Python re Module The re module provides powerful regular expression support in Python. The re.sub() method accepts a pattern, a replacement string, and a string as parameters, replacing pattern matches with the replacement string. Syntax re.sub(pattern, replacement, string) Example ...

Read More

How to extract a substring from inside a string in Python?

Yaswanth Varma
Yaswanth Varma
Updated on 24-Mar-2026 5K+ Views

In programming, a substring is a smaller part of a larger string. Extracting substrings is a common task in Python, whether you're analyzing user input, manipulating URLs, or processing text data. Python provides several methods to extract substrings from strings. Let's explore the most effective approaches. Using String Slicing The most common approach is string slicing, which uses start and end indices to extract a portion of the string. Syntax string[start:end] Example Here we extract the substring "TP" from the string "Welcome To TP" ? text = "Welcome To ...

Read More

How to check if type of a variable is string in Python?

Yaswanth Varma
Yaswanth Varma
Updated on 24-Mar-2026 32K+ Views

In Python, it is essential to verify the type of a variable before performing operations on it. For example, if you want to use string methods like upper() or lower(), you need to ensure the variable is actually a string to avoid errors. Python provides several ways to check if a variable is of type string. Let's explore the most common approaches. Using isinstance() Function The isinstance() function is the recommended approach to check variable types in Python. It takes two arguments: the variable to check and the type to check against, returning True if the variable ...

Read More

How to remove characters except digits from string in Python?

Yaswanth Varma
Yaswanth Varma
Updated on 24-Mar-2026 11K+ Views

While working with texts in Python, we often encounter strings containing a mix of characters such as letters, digits, and whitespace. In some scenarios, we may need to extract only the numeric digits from such strings. Python provides several effective approaches to accomplish this task. Using str.isdigit() with List Comprehension The isdigit() method checks whether a character is a digit. Combined with list comprehension, it provides an elegant solution to filter out non-digit characters. Syntax str.isdigit() Example Here's how to remove all characters except digits using list comprehension and str.isdigit() − ...

Read More

How to delete a character from a string using python?

Rajendra Dharmkar
Rajendra Dharmkar
Updated on 24-Mar-2026 1K+ Views

Python strings are immutable, meaning you cannot modify them directly. However, you can create new strings with specific characters removed using several methods. Here are the most effective approaches to delete a character from a string in Python. Using String Slicing String slicing allows you to remove a character at a specific position by concatenating parts before and after that position ? # Define a string text = "Hello World" # Remove character at index 4 (first 'o') new_text = text[:4] + text[5:] print("Original:", text) print("Modified:", new_text) Original: Hello World Modified: ...

Read More

How to convert string to binary in Python?

Yaswanth Varma
Yaswanth Varma
Updated on 24-Mar-2026 1K+ Views

In this article, we are going to learn about converting a string to binary, which means representing each character in the string using the binary digits 0 and 1. A binary number is a number expressed in the base-2 numerical system, which uses only two symbols - 0 and 1. Since computers store data as binary, converting strings into binary helps to understand how data is stored or transmitted. Python provides several methods to achieve this. Using ord() and format() Functions The first approach uses the Python ord() and format() functions. The ord() function retrieves the integer ...

Read More
Showing 111–120 of 25,467 articles
« Prev 1 10 11 12 13 14 2547 Next »
Advertisements