Found 10784 Articles for Python

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

Tarun Chandra
Updated on 07-Dec-2022 09:13:06

2K+ Views

In this article, we are going to find the longest common substring from more than two strings in Python. The first and only approach for finding the longest common substring is by using the Longest common substring Algorithm. We will define a function that accepts 2 strings, then we will iterate over both the strings and find for common subsequence and calculate the length. We will check entire string by iterating over and calculating the length of the sub-sequences and we will return the longest common subsequence. We will get an empty string as output if there are not any ... Read More

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

Tarun Chandra
Updated on 07-Dec-2022 09:10:29

1K+ Views

In this article, we are going to focus on checking if a string can be converted to a float in Python. The first approach is by using the float() type cast. We will write a function using exception handling and check if the given string can be converted into string, if it can be converted then True is returned, else False is returned. A floating−point number is returned by the method float() for a given number or text. It will return 0.0 as the floating−point value if no value or a blank parameter is given. Example 1 In this example, ... Read More

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

Rajendra Dharmkar
Updated on 10-Aug-2023 21:03:50

1K+ Views

To find the longest repetitive sequence in a string in Python, we can use the following approach: Iterate through each character of the string and compare it with the next character. If they are same, we can increase a counter variable and continue comparing with the next character. If they are not the same, we check if the counter is greater than the length of the longest repetitive sequence we have found so far. If it is, we update the longest repetitive sequence. Reset the counter to 1 and continue with the next character in the string. Here are three ... Read More

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

Tarun Chandra
Updated on 07-Dec-2022 09:03:12

5K+ Views

In this article, we are going to find out how to remove all special characters punctuation and spaces from a string in Python. The first approach is by using isalnum() method on each character of the string by iterating it using for loop. We will check if each character is either alphabet or number using isalnum(), if they are not then they will be removed else we will go on to next character. If every character in the string is an alphanumeric character, the isalnum() method returns True (either alphabets or numbers). If not, False is returned. Example In the ... Read More

What is the preferred way to concatenate a string in Python?

Rajendra Dharmkar
Updated on 08-May-2023 12:16:03

228 Views

The preferred way to concatenate a string in Python is by using the + operator or the join() method. Here's a step-by-step explanation of each method − Using the + operator To concatenate two strings using the + operator, simply put the strings next to each other with a + sign in between them. Example In this example, we concatenate the name variable with the greeting string using the + operator. The resulting string is "Hello, John!". name = "John" greeting = "Hello, " + name + "!" print(greeting) Output Hello, John! Using multiple + operators You ... Read More

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

Tarun Chandra
Updated on 07-Dec-2022 09:00:08

4K+ Views

In this article, we are going to find out how to extract a substring from inside a string in Python. The first approach is by using Regular expressions. A string of characters that creates a search pattern is known as a regex, or regular expression. RegEx can be used to determine whether a string includes a given search pattern. We will use re.search method of Regular Expressions and we will search for the given string that is given by the regular expression and we will extract it. Example 1 In the example given below, we are taking a string as ... Read More

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

Tarun Chandra
Updated on 07-Dec-2022 08:55:25

27K+ Views

In this article, we are going to find out how to check if the type of a variable is a string in Python. The first approach is by using the isinstance() method. This method takes 2 parameters, the first parameter being the string that we want to test and the next parameter is the keyword str. This method will return True if the given input is a string, otherwise, it returns False. It specifies that when we pass an object and class, or tuple of classes, to the isinstance() method, it will return True if the object's data type matches ... Read More

How to remove characters except digits from string in Python?

Tarun Chandra
Updated on 07-Dec-2022 08:52:54

8K+ Views

In this article, we are going to find out how to remove characters except digits from a string in Python The first approach is by using the if statement in a for loop and joining them using join() method. We will iterate the string using the for loop and then we will check whether each character is a digit or not using if statement, if it is a digit then we will go to next character, otherwise we will replace that character. For iterating repeatedly through a sequence, use a for loop. This functions more like an iterator method seen ... Read More

How to check if a string in Python is in ASCII?

Rajendra Dharmkar
Updated on 10-Aug-2023 18:01:55

938 Views

ASCII stands for American Standard Code for Information Interchange. ASCII is a character encoding system that assigns a unique number to each letter, digit, and symbol on a standard keyboard. In Python, each character has a numerical value based on the ASCII code, which can be checked using the ord() function. Understanding ASCII is important when working with text-based data and encoding or decoding text. To check if a string in Python is in ASCII, you can use the built-in string module in Python. Here are some ways you can check if a string is in ASCII: Using the isascii() ... Read More

How to delete a character from a string using python?

Rajendra Dharmkar
Updated on 10-Aug-2023 19:44:58

699 Views

Here are some examples of how to delete a character from a string using Python. Using String Slicing One of the easiest ways to delete a character from a string in Python is to use string slicing. Here's how you can do it: Example In the first line of code, we define a string called "my_string" that contains the text "Hello World". In the second line of code, we remove the character "o" by creating a new string called "new_string". We do this by slicing the original string into two parts - the characters before the "o" and the ... Read More

Advertisements