Found 10548 Articles for Python

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 do I split a multi-line string into multiple lines?

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

6K+ 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 do I remove a substring from the end of a string in Python?

Tarun Chandra
Updated on 07-Dec-2022 10:26:24

1K+ Views

In this article, we are going to find out how to remove a substring from the end of a string in Python. The first approach is by using slicing approach. In this method, we will check if the string is ending with the given substring or not, if it is ending with the given substring then we will slice the string, removing the substring. The ability to access portions of sequences like strings, tuples, and lists in Python is known as slicing. Additionally, you can use them to add, remove, or edit the elements of mutable sequences like lists. Slices ... Read More

How can I tell if a string repeats itself in Python?

Tarun Chandra
Updated on 07-Dec-2022 10:22:47

1K+ Views

In this article, we are going to find out how can we tell if a string repeats itself in Python. The first approach is by using slicing and find(). We want to see if the string we have is made up entirely of repeats of a substring of the string. We can confirm this by looking for a rotation of the string in a pair of strings. After adding a string and checking the root string in this string, except for the last and first character, we may search for the root string. This approach doesn’t work for strings with ... Read More

How to do string concatenation without \\'+\\' operator in Python?

Yaswanth Varma
Updated on 07-May-2025 16:49:24

2K+ Views

In this article, we are going to find out how to do string concatenation without the plus operator in Python. Generally, String concatenation is the process of joining two or more strings into one, and the common way to do this is by using the '+' operator. For example, writing "Welcome" + " " + "TP" results in the string "Welcome TP". But here we need to perform the concatenation without the '+' operator. For achieving this, Python provides alternative methods, which we will explore one by one. Using Python join() Method The first approach is ... Read More

Can we do math operation on Python Strings?

Rajendra Dharmkar
Updated on 10-Aug-2023 11:54:19

4K+ Views

You can perform some math operations on Python strings technically, but it may not work the way you expect it to. Here are a few examples: Concatenating Strings with + Example In this example, we concatenate two strings by using the + operator. This works because the + operator is overloaded in Python to work with strings as well as numbers. When we use the + operator with strings, it concatenates them together to form a single string. string1 = "Lorem " string2 = "Ipsum" result = string1 + string2 print(result) Output Lorem Ipsum Multiplying Strings with * ... Read More

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 to convert a Python csv string to array?

Yaswanth Varma
Updated on 07-May-2025 17:07:05

16K+ Views

The comma-separated values (CSV) is a text format that is used for storing tabular data. In Python, we will come across situations where CSV data is available in string format. For performing operations on this data, we need to convert it into an array. Python provides multiple ways to achieve this conversion. In this article, we are going to learn about converting the CSV string into an array. Using Python split() Method In this approach, we are going to use the Python split() method, which is used to split all the words in a string separated by ... Read More

How to remove a list of characters in 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

How to get integer values from a string in Python?

Tarun Chandra
Updated on 07-Dec-2022 10:05:47

12K+ Views

In this article, we are going to find out how to get integer values from a string in Python. The first approach is by using the filter() method. We will pass the string and the method isdigit() to the filter method. Python has a built-in function called Filter(). An iterable, like a list or dictionary, can have the filter function applied to it to create a new iterator. Based on the criteria you provide, this new iterator can filter out specific elements quite well. The filter() method checks the string for digits and filters out the characters that satisfy the ... Read More

Advertisements