Operators and Strings in Python

Manish KumarManish Kumar
4 min read

Introduction

Operators are the heart of any programming language. They help in data manipulating so that execution can be perform effectively and efficiently. Operators control the flow of program and play key role in logic building.

Types of Operators

1. Arithmetic Operators

As the name suggests it performs all the basic mathematical calculations including addition, subtraction, division and multiplication. Few are advanced mathematical operations are also there like modulo, and exponentiation.

2. Assignment Operators

This operators performs action with the right operand and assign it value to the left operand.

3. Comparison Operators

It compares two values and based on the given condition returns the boolean value.

4. Logical Operators

It compare multiple conditional statements and returns the boolean value. There are three logical operators AND, OR & NOT . It executes result according to the truth tables.

Example of AND, OR, NOT

a) In first eg: x = 5 so first it will check x > 3 → True then it will check x < 10 → True. So according to truth table both condition are True. True represents as 1 so it will execute 1 i.e. True

b) In second eg: x = 5 so first it will check x > 3 → True then it will result in True because according to truth table atleast one condition needs to be True for condition should be fulfilled. If somehow first condition becomes False then it will go beyond to check for second condition.

c) In last eg: both condition x > 3→ True and x < 10 → True but after that NOT in-place according to truth table condition True becomes False and vice versa. So final outcome will be False.

5. Bitwise Operators

It performs bitwise on numbers. First it will converted the number into binary form then perform bitwise calculations. List of Bitwise operators are:- Bitwise AND (&), Bitwise OR (|), Bitwise NOT (~), Bitwise XOR (^), Bitwise right shift (>>), Bitwise left shift (<<)

6. Membership Operator

Membership operator are used to check whether particular sequence is present in an object. The in operator returns True if particular sequence is present otherwise False. Whereas not in operator returns True when particular sequence is not present otherwise False.

7. Identity Operator

Identity operator are used to check memory locations of a variable. The is operator returns True if variable pointed to the same memory location otherwise False. Whereas is not operator returns True if variable pointed to the different memory location otherwise False.

Strings in Python

Strings is a sequence of characters enclosed in either single quotes (‘‘) or double quotes (““). Strings are immutable i.e. whenever strings are created they cannot be modified or changed.

Eg: str1 = “Hello World“

Immutable Vs Mutable Objects

Immutable objects like strings, tuples, integers and floats once created cannot be changed. If user try to modify an immutable objects python create another memory address for that objects and previous one remains unchanged.

Mutable objects like list, sets and dictionaries once created can be changed. If user try to modify a mutable object the value in-place is changed but the memory address for that objects remains the same.

Strings In-built Methods

1. length of String

2. Slicing

syntax: string [start: end: step-size] default value of start →0, end →length of string-1, step-size → +1

3. Upper, Lower, Capitalize, Title

string.upper() → Each and every letter of the sentence in the capital letters.

string.lower() → Each and every letter of the sentence in the smaller letters.

string.capitalize() → First letter of the first word in sentence in capital letters.

string.title() → First letter of each and every word present in sentence in capital letters.

4. String Concatenation

5. Comparing Strings

6. Replace

7. Split

8. String Formatting

9. Strip

Strip method removes any whitespace from the beginning or the end.

10. Index and Find

It returns index if the substring present in the main string but returns ValueError if subtring not present. Whereas find() method returns index if substring is present in the main string else returns -1.

Index Method

Find Method

11. Count

12. isalpha(), isupper(), islower(), isdigit()

isalpha() → returns True if all characters are alphabet in the main string else returns False.

isupper() → returns True if all characters are in uppercase else returns False.

islower() → returns True if all characters are in lowercase else returns False.

isdigit() → returns True if all characters are digits else returns False.

13. Swapcase

The swapcase() method is in-built function in python that inverts the uppercase into lowercase and vice-versa and keeps non-alphabetic remains unchanged.

0
Subscribe to my newsletter

Read articles from Manish Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Manish Kumar
Manish Kumar