Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by Rohan Singh
Page 11 of 15
Python Program to Differentiate String == operator and__eq__() method
In Python, the comparison operator == and __eq__() method are closely related but serve different purposes when working with strings and objects. The == operator provides default comparison behavior, while __eq__() allows custom equality logic. Understanding their differences is crucial for effective string comparison in data analysis and object-oriented programming. == Operator in Python The == operator is used to compare two values for equality. It returns True when values are equal and False when they differ. For strings, it compares content regardless of memory location ? str1 = "Hello World" str2 = "Hello World" str3 ...
Read MorePython Program to demonstrate the string interpolation
In Python, we can demonstrate string interpolation using f-strings, the % operator, and the format() method. String interpolation is the process of inserting dynamic data or variables into a string, making it useful for creating formatted strings without manual concatenation. Method 1: Using f-strings An f-string is a string literal that starts with f or F. The prefix indicates that the string contains expressions enclosed in curly braces {}, which are evaluated at runtime. Example Here we create variables and use an f-string to interpolate their values into a formatted message ? name = ...
Read MorePython Program to create a String object
In Python, we can create a string object using Python's built-in function str() and also by assigning a sequence of characters to a variable. The sequence of characters is enclosed in single quotes, double quotes, or triple quotes for multiline strings. In this article, we look at the various ways to create string objects in Python. Using Single Quotes We can create a string object by simply assigning a sequence of characters enclosed in single quotes to a variable − my_string = 'Hello World!' print(my_string) print(type(my_string)) The output of the above code is − ...
Read MorePython Program to compare two strings by ignoring case
In Python, we can compare two strings while ignoring their case using several approaches. Strings are character sequences that can contain uppercase and lowercase letters. When comparing strings case-insensitively, we need to normalize both strings to the same case before comparison. Using lower() Method The most common approach is converting both strings to lowercase using the lower() method before comparison ? string1 = "Hello" string2 = "hello" if string1.lower() == string2.lower(): print("The strings are equal, ignoring case.") else: print("The strings are not equal, ignoring case.") ...
Read MorePython Program to Clear the String Buffer
In Python, a string buffer is a mutable sequence of characters that can be modified before writing to an output stream. Python's StringIO class from the io module provides an in-memory buffer for string operations. We can clear this buffer using different methods depending on our requirements. Method 1: Using truncate() and seek() The truncate(0) method removes all content from the buffer starting at position 0, while seek(0) resets the cursor to the beginning ? from io import StringIO # Create a string buffer buffer = StringIO() # Add some text to the buffer ...
Read MorePython Program to Check if a string is a valid shuffle of two distinct strings
In Python, we can check if a string is a valid shuffle of two distinct strings by comparing the sorted characters. A valid shuffle means that a string is formed by mixing characters from two distinct strings while maintaining the relative order of characters from each original string. What is a Valid Shuffle? A valid shuffle combines characters from two strings without adding, removing, or changing any characters. Let's see some examples ? S1: "abc" S2: "def" Valid shuffles: "adbecf", "dabecf", "abdefc" Invalid shuffles: "abgfcd" (contains 'g'), "tabcde" (contains 't') ...
Read MoreDifference between \'and\' and \'&\' in Python
In Python 'and' and '&' both are used to perform logical operations, but they work differently. The and operator performs logical AND operations, while the & operator performs bitwise AND operations. Understanding their differences is crucial for writing correct Python code. Key Differences Feature and operator & operator Purpose Logical operations Bitwise operations Return Type Boolean or operand value Integer value Evaluation Short-circuit evaluation Evaluates all operands Operation Level Works on truthiness Works on binary representation The 'and' Operator The and operator performs logical ...
Read MoreDifference Between ‘+’ and ‘append’ in Python with examples
In Python, the + operator is used to concatenate two lists or strings together and return a new object, whereas the append() method is used to add elements to the end of an existing list. The + acts as an operator whereas append() is a method. In this article, we will understand the differences between the + operator and the append() method in Python. Key Differences Aspect + operator append() method ...
Read MoreDifference between \'__eq__\' VS \'is\' VS \'==\' in Python
In Python, object comparison can be performed using three different approaches: the __eq__ method, the is operator, and the == operator. Each serves a distinct purpose in determining equality or identity between objects. Overview of Comparison Methods Method Purpose Checks Usage __eq__ Custom equality logic Object values (customizable) Class method definition is Identity comparison Memory location a is b == Value comparison Object values a == b The __eq__() Method The __eq__ method allows you to define custom equality logic for your classes. When you use ...
Read MoreDifference between != and is not operator in Python
The != operator checks if the values of two objects are different, while the is not operator checks if two objects are not the same object in memory (different identity). Understanding this difference is crucial for proper Python comparisons. Key Differences != operator is not operator Compares values of objects Compares object identity (memory location) Returns True if values are different Returns True if objects have different identities Syntax: object1 != object2 Syntax: object1 is not object2 Example with Different Data Types Let's compare integers, strings, ...
Read More