Check Variable Type as String in Python

Yaswanth Varma
Updated on 20-May-2025 11:51:21

32K+ Views

In this article, we are going to find out how to check if the type of a variable is a string in Python. It is necessary to verify the type of a variable before performing the operations. For example, if we want to ensure that the variable is a string before using the string methods like upper(),  lower(). If the variable is not of the expected type, calling these methods will raise an error. Python provides various ways to check if the variable is of type string. Let's dive into the article to learn more about it. ... Read More

Extract Substring from a String in Python

Yaswanth Varma
Updated on 20-May-2025 11:44:44

4K+ Views

In programming, a substring is a smaller part of the string. Extracting the substring is a task in Python, whether we are analysing the user input or manipulating URLs, we will find ourself to grab a portion of the string. For achieving, Python provides the various way, Let's dive into the article to learn more about extracting the substring from the string. Using Python slicing The first approach is by using the Python slicing, It is the most common way to extract the substrings. Simply, we require the start and end indices. Example In the following example, ... Read More

Extract Date from a String in Python

Yaswanth Varma
Updated on 20-May-2025 11:41:54

18K+ Views

In Python, Extracting dates from the strings is the common task in the data preprocessing and text analysis, especially when dealing with user inputs. Dates can appear in many formats like DD-MM-YYYY, YYYY/MM/DD or in text formats like April 25, 2025. Python offers various libraries and tools to achieve this task. In this article we will explore the different ways to extract dates from the strings. Using the Python "re" Module The python re module supports regular expressions, helping in string pattern matching and manipulation. In this approach, we are going to use the regular expression pattern to ... Read More

Remove List of Characters from String in Python

Yaswanth Varma
Updated on 20-May-2025 11:16:33

5K+ Views

In Python, Strings are the immutable sequences of characters. While working with them, we will come across situations where we need to remove the unwanted characters (like punctuation, special symbols, etc.). In this article, we are going to find out how to remove a list of characters from a string. Python provides several ways to achieve this using the built-in methods and list comprehensions. Using Python str.replace() Method The python str.replace() method is used to replace all occurrences of one substring in a string with another substring. This method takes 2 parameters: the character ... Read More

Find nth Occurrence of Substring in a String in Python

Yaswanth Varma
Updated on 20-May-2025 11:00:51

5K+ Views

While working with the strings, the common requirement is searching for a specific substring within a larger string. Python provides built-in methods that allow you to locate the first occurrence of a substring. But what if the substring appears multiple times. For example, consider the string "Welcome to the Tutorialspoint to". If we look for the second occurrence of the substring "to", using the find() will get only the first match, but we need an approach that searches beyond the first occurrence, counting each one until the nth. In this article, we will explore how to find the nth ... Read More

Split Multi-Line String into Multiple Lines

Yaswanth Varma
Updated on 20-May-2025 10:54:47

7K+ Views

A string is a collection of characters that may be used to represent a single word or an entire phrase. Strings are useful in Python since they don't need to be declared explicitly and may be defined with or without a specifier. In this article, we are going to find out how to split a multi-line string into multiple lines in Python. While working with the strings in programs, we will come across multi-line strings. To process such strings one by one, we need to split the string into individual lines. In this article, we will explore different methods. ... Read More

Convert String to List of Words in Python

Yaswanth Varma
Updated on 20-May-2025 10:36:54

10K+ Views

Strings are one of the most commonly used data types. In this article, we are going to find out how to convert a string to a list of words in Python. Converting the string into a list is helpful when dealing with user inputs or when we want to manipulate individual words in a string. Python provides several ways to achieve this. Let's explore them one by one. Using Python str.split() Method Python str.split() method accepts a separator as a parameter, splits the string at the specified separator, and returns the result as a list. If we use the split() ... Read More

Sort Letters in a String Alphabetically in Python

Yaswanth Varma
Updated on 20-May-2025 10:28:32

7K+ Views

In Python, Sorting the string alphabetically is a common task helps for string manipulation or text analysis. It is used in creating the anagrams, checking for permutations, or standardizing the input for consistent processing, for example, turning 'banana' into 'aaabnn'. In this article, we will explore how to sort the letters in a string alphabetically, this can be done by using the Python built-in function. Using Python sorted() Function The Python sorted() function is used to return a new sorted list from the items in the iterable object. The order of sorting can be set to either ascending ... Read More

Use StringBuffer Instead of String in Java

raja
Updated on 19-May-2025 19:59:59

971 Views

In Java, both String and StringBuffer classes are used to represent sequences of characters. However, the String class is immutable, which means once a String object is created, its value cannot be changed, while the StringBuffer class is mutable. It allows us to modify the contents of the string without creating a new object. In this article, we will discuss why we should use the StringBuffer class instead of String in Java. Why Use StringBuffer Instead of String? A few reasons why StringBuffer is often preferred over String is given below: A StringBuffer is thread-safe ... Read More

String Literal Storage in String Constant Pool in Java

Shriansh Kumar
Updated on 19-May-2025 19:55:30

2K+ Views

In Java, the string literals (or, string objects) are stored in a separate memory area called string constant pool to improve the performance of string operations and optimize the memory while using them. Let's understand how. Creating String Objects in Java There are two ways to create a String object in Java: Using the new operator Using String literal Example The example given below shows how to create a string object: public class Example { public static void main(String[] args) { ... Read More

Advertisements