Operators and Strings in Python


Operators in Python
Operators in the Python programming language help you work with and manipulate data effectively. There are seven (7) different operator categories in Python, and you will learn about each in this article with the help of examples.
Types of Python Operators
There are seven operators in Python:
Arithmetic Operators
Assignment Operators
Comparison Operators
Logical Operators
Bitwise Operators
Identity Operators
Membership Operators
Arithmetic Operators
The arithmetic operator is an operator that helps to perform a mathematical operation.
like →
a=10
b=5
print(a+b) # addition
print(a-b) # subtraction
print(a*b) # multiplection
print(a**b) # exponcience
print(a/b) # division
print(a//b) # flot division
print(a%b) # modulor
Assignment Operators
Assignment operators are used to assign values to variables.
This operator assigns the value of the expression’s right side to the operand on the left side.
There are eight types of assignment operators in Python.
a = 10 # Assign 10 to a
b = 5 # Assign 5 to b
a += b # Equivalent to a = a + b
print("a += b:", a)
a -= b # Equivalent to a = a - b
print("a -= b:", a)
a *= b # Equivalent to a = a * b
print("a *= b:", a)
a /= b # Equivalent to a = a / b
print("a /= b:", a)
a //= b # Equivalent to a = a // b
print("a //= b:", a)
a %= b # Equivalent to a = a % b
print("a %= b:", a)
a **= b # Equivalent to a = a ** b
print("a **= b:", a)
Comparison Operators
Also known as relational operators, they are used to compare different values or variables. These operators only return True or False according to the condition.
There are six types of comparison operators, namely.
a = 10
b = 5
print(a == b) # Equal to
print(a != b) # Not equal to
print(a > b) # Greater than
print(a < b) # Less than
print(a >= b) # Greater than or equal to
print(a <= b) # Less than or equal to
Logical Operators
Logical operators are used to combine conditional statements.
There are three logical operators in Python.
a = 10
b = 5
print(a>5 and b<10) # True and True -> True
print(a<5 or b<10) # False or True -> True
print(not(a == b)) # not(False) -> True
Bitwise Operators
work at the binary (bit) level, comparing individual bits of numbers.
These operators perform bit-by-bit operations on specified values.
There are six Python bitwise operators:
a = 10 # Binary: 1010
b = 5 # Binary: 0101
print(a & b) # Bitwise AND
print(a | b) # Bitwise OR
print(a ^ b) # Bitwise XOR
print(~a) # Bitwise NOT
print(a << 1) # Left shift by 1
print(a >> 1) # Right shift by 1
Identity Operators
Identity operators are used to compare the memory location of two objects. The output from an identity operator is always a Boolean value, meaning it’s either True or False.
There are two types of identity operators in Python.
a = 10
b = 5
print(a is b)
#output is False
print (a is not b)
#output is True
Membership Operators
Membership operators are used to check if a character, substring, value, or variable is present in a sequence. This sequence could be a string, a tuple, a list, or a set.
There are two types of membership operators in Python.
my_str1 = "A"
my_str2 = "Ajit"
print('3' in my_str1) # false
print('6' not in my_str1) #True
print('i' in my_str2) # True
print('z' not in my_str2) # True
Strings in Python
What Are Strings in Python?
A string is a sequence of characters enclosed in either single quotes (‘ ‘) or double quotes (“ “), triple quotes(‘‘‘ ‘‘‘) or (“““ “““). Strings can contain letters, numbers, symbols, spaces, messages, or any text in your code.
Example:- "Hello, World!" is a string.
single = 'Hello'
double = "World"
multi ="""This is
a multiline string"""
Creating Strings
You can create strings in Python using single or double quotes, to create a string.
# Using single quotes
single_quoted_string = 'This is a string with single quotes'
# Using double quotes
double_quoted_string = "This is a string with double quotes"
Python provide multiple operations and methods for working with strings.
Changing Case: .lower( ) and .upper( )
name = "Ajit"
print(name.lower()) # ajit
print(name.upper()) # AJIT
Replace Text: replace( )
original_string="hello world"
new=original_string.replace("world", "ajit")
print(new)
Split a String: split ( )
string="Hello world"
string.split()
Formatted Strings: f-strings ( formatted string literals )
age = 20
text = f"My name is Ajit, I am {age} years old"
print(text)
Stripping Whitespace: Stripping Whitespace
name = " sohan kumar das "
print(name.strip()) # Removes (start + end) spaces
print(name.lstrip()) # Removes (left side ) space
print(name.rstrip()) # Removes (right side) space
Finding Index: index()
string = "Hello, World!"
index = string.index("W")
print(index)
index= string.index("d")
print(index)
Finding Index: find()
string = "Hello, World!"
index = string.find("W")
print(index)
index= string.find("d")
print(index)
Counting: count()
sentence = "this is a sample sentence"
count=sentence.count("s")
print(count)
length of String: .len()
text = "Hello, world!"
length = len(text)
print(length)
Joining Strings: join()
lines = ["Line 1", "Line 2", "Line 3"]
print("\n".join(lines))
resource: - Strings , Operators
Subscribe to my newsletter
Read articles from Ajit Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
