How to find the 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

How do I split a 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

How to convert a string to a list of words in python?

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

9K+ 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

How to sort the 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

Why should we use a StringBuffer instead of a String in Java?\

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

925 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

Why String literal is stored 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

What are the differences between printStackTrace() method and getMessage() method in Java?

Shriansh Kumar
Updated on 19-May-2025 19:36:14

5K+ Views

The errors that occur during runtime after compilation are called exceptions. To handle and rectify these errors, it is necessary to understand the cause and the point where the error occurs. There are two ways to find the details of the exception: one is the printStackTrace() method, and another is the getMessage() method. Both are used for exception handling in Java, but they are different from each other. Let's discuss how. The printStackTrace() Method The printStackTrace() method is defined in Throwable class of java.lang package and it is inherited into both java.lang.Error class and java.lang.Exception class. When you use it ... Read More

How do I convert a datetime to a UTC timestamp in Python?

SaiKrishna Tavva
Updated on 19-May-2025 17:56:03

36K+ Views

We can use the datetime module to convert a datetime to a UTC timestamp in Python. If we already have the datetime object in UTC, then the timestamp() function can be directly used to get a UTC timestamp. This function returns the time since epoch for that datetime object. If we have the datetime object in the local timezone, first replace the timezone info and then fetch the time. The following are the various methods to convert a datetime object into a UTC timestamp in Python. Using datetime.timestamp() with UTC-aware datetime Local ... Read More

How to compare calendar.timegm() vs. time.mktime() in Python?

SaiKrishna Tavva
Updated on 19-May-2025 17:37:30

1K+ Views

In Python, the mktime() function (from the time module) assumes that the passed tuple is in local time, while the calendar.timegm() (from the calendar module) assumes it's in GMT/UTC. Depending on the interpretation, the tuple represents a different time, so both functions return different values (seconds since the epoch are UTC-based). The difference between the values should be equal to the time zone offset of your local time zone. Understanding time.mktime() in Local Time Context The Python time.mktime() method converts the object form of local time into seconds since the epoch (January 1, 1970, 00:00:00 UTC). This method is the inverse function of localtime() and ... Read More

When to use @JsonValue annotation using Jackson in Java?\

Manisha Chand
Updated on 19-May-2025 16:23:29

5K+ Views

In Java, Jackson is a library that is used to convert JSON to Java objects and vice versa. Jackson Annotations are used during serialization and deserialization. We use these to denote or specify annotations before a particular field or method (that is declared in Java). Using an annotation before a field, we can denote whether it is a variable, is a JsonProperty, should be ignored, or what condition should be applied to it. So basically, Annotations make JSON output clearer as we required. In this Article, we will learn about ... Read More

Advertisements