A string is a collection of characters that can represent a single word or a whole sentence. Unlike Java, there is no need to declare Python strings explicitly; we can directly assign a string value to a literal. While working with the string, we will encounter situations to check whether the string starts with a capital letter. This can be useful for validating the names or sentences to ensure they follow certain formatting rules. Python provides multiple methods to test this condition. In this article, we will explore how to test if a string with a capital letter. ... Read More
Concatenation is nothing but the process of joining two or more strings together. In Python, if one value is a number (integer or float), we can't directly concatenate it with a string using the '+' operator, as it raises a TypeError. Python does not allow the implicit conversion between strings and numbers during the concatenation. So, we need to explicitly convert the numbers to a string. Using Python str() Function The Python str() function is used to convert the given value into a string. In this approach, we are going to apply the str() function to ... Read More
While working with the texts in Python, we will find the strings that contain a mix of characters such as letters, digits, and white-space. In some scenarios, we may need to extract only the numeric digits from such strings. For achieving this, Python provides various ways. Let's explore one by one - Using Python str.isdigit() Method The first approach is by using the Python isdigit() method. It is used to check whether the character is a digit. It returns true if all the characters in the input string are digits. Syntax Following is the syntax for Python str.isdigit() ... Read More
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
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
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
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
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
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
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