In Python, A 3D list is also called a three-dimensional array (list of lists of lists). It can be visualized as a cube or a set of tables stacked together. It is commonly used to represent data with three indices. For example, a matrix of images (height, width, depth). In this article, we are going to learn how to create a 3D list. As Python does not have built-in support for multi-dimensional arrays like other programming languages, but using the nested lists and loops, we can create and manipulate 3D lists. Creating a 3D List In the following example, ... Read More
In this article, we are going to learn how to remove all the duplicate words from the given sentence. Generally, we will encounter situations where the sentence contains the same word repeated multiple times. These duplicate words make the text look messy and can affect further processing. Removing duplicates will help to improve the readability and ensure each word appears only once in the final sentence. Using Python split() Method The Python split() method is used to split all the words in the string using the specified separator. The separator can be a comma, full-stop, or any ... Read More
In this article, we are going to learn how to check if the number is positive, negative, odd, even, or zero. Identifying and categorizing a number based on its characteristics is a basic operation. A number can fall into multiple categories: Positive: A number is greater than 0. Negative: A number less than 0. Zero: The number that is equal to 0. Even: The number that is divisible by 2. Odd: The number that is not divisible by 2. Let's dive into the ... Read More
String concatenation is the process of joining two or more strings together. It is important to choose the right method for string concatenation when dealing with building output, logging messages, etc. While Python has several methods to concatenate strings, not all the methods perform equally, especially incase of nested loops. In this article, we will explore the different methods of string concatenation in Python. Using Python + Operator The first approach is by using the + operator, which is the straightforward way to concatenate strings. This method is inefficient in loops because it creates a new string object ... Read More
In Python, the backslash-escaped string contains the characters that are followed by backslash(\), which are used as escape characters. For example, represents the literal characters "" and "n" (not a new line). In some scenarios, we need to unescape such a string, i.e, convert these sequences back to their intended special characters or read them as normal text. Python provides several ways to achieve this. In this article, we will explore how to un-escape a backslash-escaped string. Using Python .decode() Method The Python decode() method is used to decode the string using the codec ... Read More
Wrapping the string means formatting or breaking the string so that it fits within the specified width. It is useful when writing the content to the file, such as logs or console-like outputs. Python provides various ways to achieve string wrapping before writing it to the file using the built-in methods. In this article, we are going to learn how to wrap a string in a file in Python. Using Python textwrap.wrap() Method The textwrap.wrap() method is used to wrap a single paragraph of text. so that every line is at most a specified length. It accepts a text ... Read More
In this article, we are going to learn how to convert a string to JSON in Python. JSON is a lightweight data interchange format that is used to read and write, and store data. Usually, we receive JSON data in the form of a string. To process it using a Python program, we need to convert the JSON string into an object, which can be stored in Python dictionaries and lists. This conversion can be done by using functions provided by the Python JSON module. Using Python json.loads() Function The Python json.loads() function is used to convert the JSON-formatted ... Read More
The pointers can be directly compared using relational operators. In this article, we will learn about the comparisons of the pointers with the help of examples. Pointers Comparison in C and C++ In C/C++, we can directly compare the pointers using relational operators (==, !=, , =). These operators are used to compare two variables, values, or pointers. It returns a Boolean true value when the comparison is correct otherwise, it is false. The core syntaxes of C and C++ pointers are similar such as declaration, dereferencing, and pointer arithmetic. All of these are identical in behavior and still use ... Read More
In C, you cannot use a function call on the left side of an assignment if it returns a value because function calls return non-assignable values. For example, function_name() = value; However, if the function returns a pointer, you can dereference it to assign a value. For example, *function_name() = value; In C++, the same rule applies. You can use the returned reference/pointer on the left side to modify the original variable which is valid only if it returns a reference. function_name() = value; Note: References allow direct access to variables, and pointers allow indirect access via dereferencing. ... Read More
ASCII stands for the American Standard Code for Information Interchange. It represents the characters using the integer value from 0 to 127. In this article, we are going to explore the different ways to check if a string contains only ASCII characters in Python. It is necessary to verify whether a string contains only ASCII characters when ensuring compatibility. Using Python isascii() Method The Python isascii() method is a built-in method in Python 3.7. It returns true if all the characters in the string are ASCII; otherwise false. Syntax Following is the syntax for the Python isascii() method - ... Read More