In this article, we are going to learn about converting a string to binary, which means representing each character in the string using the binary digits 0 and 1. A binary number is a number expressed in the base2 numerical system, which uses only two symbols - 0 and 1. Since computers store data as binary, converting the strings into binary helps to understand how data is stored or transmitted. Python provides several methods to achieve this. Using Python ord() and format() Functions The first approach is by using the Python ord() and format(), Where the Python ord() ... Read More
Sorting the string that contains the number, such as ("xy1", "xy2", "xy10"), can be complex in Python. For example, if we sort the list ["xy1", "xy2", "xy10"] using the built-in sort() method, it results in ["xy1", "xy10", "xy2"]. But we will expect "xy2" to come before "xy10". It is because the Python default sorting uses the lexicographical order and compares the characters from left to right based on their Unicode values. Since the character '1' in "xy10" comes before "2" in "xy2", the "xy10" is treated as smaller, even though the number 10 is greater than 2. This ... Read More
In Python, we will come across situations where we encounter long lines of code that exceed the recommended line length of 79 characters (suggested by the Python style guide). To improve code readability, Python provides several ways to wrap long lines. In this article, we will explore the various methods to wrap long lines in Python. Using Backslash(\) The Backslash(\) is used as the line continuation character in Python. It indicates the compiler that the statement continues on the next line. If we try to place anything (word, space or comment) after the backslash, it will result in ... Read More
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
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP