Found 10476 Articles for Python

Python Program to Differentiate String == operator and__eq__() method

Rohan Singh
Updated on 17-Apr-2023 10:30:38

292 Views

In Python, the comparison operator (==) and equals() methods are used in different ways when working with strings. To differentiate between the == operator and the equals method in Python we have to use them with string comparison. String comparison widely occurs when we work with strings in data analysis and machine learning. In this article, we will see how we can differentiate between the == operator and the equals() method when used with strings. == operator in Python The == is a comparison operator used to compare two string values. It returns True when the value of the string ... Read More

Python Program to demonstrate the string interpolation

Rohan Singh
Updated on 17-Apr-2023 10:29:01

240 Views

In Python, we can demonstrate string interpolation using the f-string, %operator, and format() method. String Interpolation is the process of inserting dynamic data or variables in the string. It is useful when a string is formed using variables or expressions without using any string formatting or string concatenation. In this article, we will see how we can use Python to do string interpolation. Method 1 : Using f-string An f-string is a string literal which starts with f or F. The prefix f or F indicates that the string is an f-string. The string contains the expression which is enclosed ... Read More

Python Program to create a String object

Rohan Singh
Updated on 17-Apr-2023 10:27:45

1K+ Views

In Python, we can create a string object using Python's inbuilt function str() and also by assigning a sequence of characters to a variable. The sequence of characters is enclosed in the single quote or double quotes. We can also create a multiline string object using triple quotes. In this article, we look at the various ways in which we can create a string object in Python. Example 1:Creating a string object using single quote We can create a string object by simply assigning a sequence of characters enclosed in a single quote to a variable. my_string = 'Hello World!' ... Read More

Python Program to compare two strings by ignoring case

Rohan Singh
Updated on 17-Apr-2023 10:13:59

10K+ Views

In python we can use the comparison operators like “==”, ”!=”, “”, ”=” and python inbuilt function like lower() and upper() methods to compare two strings by ignoring case. Strings are character sequences enclosed in double quotes. These operators compare strings based on the Unicode code points assigned to each character of the string. In this article, we will understand how we can compare two strings by ignoring cases of the string. Comparing Strings Ignoring Case To compare two strings in Python while ignoring the case, we can use the lower() or upper() function which converts the string to ... Read More

Python Program to Clear the String Buffer

Rohan Singh
Updated on 17-Apr-2023 10:12:53

4K+ Views

In Python, we can clear the string buffer by assigning an empty string to the buffer and using Python's inbuilt reset() method. A string buffer is a mutable sequence of characters. These characters can be modified according to the requirements. The string buffer can be modified or cleared before the string is written to an output stream. In this article, we will understand how to clear the string buffer using examples. Method 1 : Assign Empty String to the Buffer One of the methods to clear the string buffer is to assign the buffer with an empty string. Assigning an ... Read More

Python Program to Check if a string is a valid shuffle of two distinct strings

Rohan Singh
Updated on 17-Apr-2023 10:11:07

530 Views

In Python, we can use inbuilt functions like len() and logic to check the sequence of the characters in the resultant string to know if a string is a valid shuffle of two distinct strings. A valid shuffle of two distinct strings means that a string is formed by mixing the characters of two distinct strings while maintaining the order of the characters of the two original strings. Python provides various inbuilt functions to play with strings. In this article, we will check if the string formed by shuffling the characters of two distinct strings is a valid string or ... Read More

Difference between 'and' and '&' in Python

Rohan Singh
Updated on 17-Apr-2023 10:06:17

5K+ Views

In Python ‘and’ and ‘&’ both are used to perform logical operations. The and-operator is used to perform logical AND operation whereas the & operator is used to perform bitwise AND between two expressions. In this article, we will explore the differences between the two operators and how to use them in Python. and operator & operator Used for logical operations Used for bitwise operations Returns boolean value Returns integer value Evaluates both operands Compares the binary representation of operands Short circuits if the first operand is false Perform operation ... Read More

Difference Between ‘+’ and ‘append’ in Python with examples

Rohan Singh
Updated on 17-Apr-2023 09:52:11

2K+ Views

In Python, the + operator is used to concatenate two lists or strings together and return a new string whereas the append operator is used to add elements to the end of an existing string. The + acts as an operator whereas append() is a method in Python. In this article, we will understand the differences between the + operator and the append() method in Python. + operator append() method Purpose concatenation adds an element to the end Type operator method Input Two or more strings/list One element output ... Read More

Difference between __sizeof__() and getsizeof() method in Python

Rohan Singh
Updated on 17-Apr-2023 09:50:31

5K+ Views

The __sizeof__() method and the getsizeof() method both are used to get the size of the objects used in the program. The getsizeof() method returns an additional overhead for garbage collection along with each element size of the list. The __sizeof__() method returns the actual size of the object without any overhead. In this article, we will see how we can differentiate these operators in Python. __sizeof__() operator getsizeof() The __sizeof__() operator returns the size of the object without any extra overhead for garbage collection. The getsizeof() operator returns the size of the object with extra ... Read More

Difference between '__eq__' VS 'is' VS '==' in Python

Rohan Singh
Updated on 17-Apr-2023 10:09:42

8K+ Views

The __eq__, ‘is’ and == operators in Python are used to compare the equality of objects in Python. The __eq__ method checks for the equality of objects of the same class or if we want to make a custom comparison function. The ‘is’ operator checks for the memory location of the two objects whereas the == operator checks only the value of the two objects being compared. In this article, we will discuss the difference between the three operators and their uses. Method Functionality Syntax __eq__ Checks for equality of objects values between two objects of ... Read More

Advertisements