Python Articles

Page 788 of 855

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

How to split string on whitespace in Python?

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

Sometimes we need to split a string into individual words by separating them at whitespace characters. Python provides several methods to accomplish this task efficiently. Using the split() Method The simplest approach is using the built-in split() method. When called without arguments, it automatically splits on any whitespace character (spaces, tabs, newlines) and removes empty strings from the result ? text = "Hello Everyone Welcome to Tutorialspoint" print("The given string is:") print(text) print("The strings after the split are:") result = text.split() print(result) The given string is: Hello Everyone Welcome to Tutorialspoint ...

Read More

How can I concatenate str and int objects in Python?

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 24-Mar-2026 917 Views

In many coding languages, when you try to combine a string with a number, the number is usually converted into a string automatically so both parts can be joined together. This is called implicit type conversion. With the + operator for concatenating a string with a numerical value, Python does not perform the implicit type conversion. Instead, it throws an error (specifically a TypeError) because it expects both operands to be strings. The following are the various methods to do it: Using the str() Function Using f-strings Using ...

Read More

How do I un-escape a backslash-escaped string in Python?

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

In Python, a backslash-escaped string contains characters preceded by a backslash (\) that represent escape sequences. For example, represents literal backslash and "n" characters (not a newline). Sometimes you need to unescape these strings to convert them back to their intended special characters. Python provides several methods to achieve this. Let's explore the most effective approaches. Using the decode() Method The decode() method can convert escape sequences back to their special characters using the unicode_escape encoding ? Syntax string.encode().decode('unicode_escape') Example escaped_string = "Welcome ToTutorialsPoint" result = escaped_string.encode().decode('unicode_escape') print(result) ...

Read More
Showing 7871–7880 of 8,546 articles
« Prev 1 786 787 788 789 790 855 Next »
Advertisements