0% found this document useful (0 votes)
2 views

Python_Cheat_Sheet_Explained_Strings_OOPs

This document is a cheat sheet covering Python strings and Object-Oriented Programming (OOP) concepts. It includes string basics, methods, slicing, formatting, and OOP principles such as classes, inheritance, encapsulation, polymorphism, and abstraction. Additionally, it provides exam preparation tips for mastering these topics.

Uploaded by

kirthikkumar0012
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Python_Cheat_Sheet_Explained_Strings_OOPs

This document is a cheat sheet covering Python strings and Object-Oriented Programming (OOP) concepts. It includes string basics, methods, slicing, formatting, and OOP principles such as classes, inheritance, encapsulation, polymorphism, and abstraction. Additionally, it provides exam preparation tips for mastering these topics.

Uploaded by

kirthikkumar0012
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Python Cheat Sheet: Strings & OOPs (With Explanations)

PYTHON STRINGS AND OOPS CHEAT SHEET (WITH EXPLANATIONS)

====================================
SECTION 1: STRINGS IN PYTHON
====================================

1. String Basics:
-----------------
s = "hello world"
- Strings are sequences of characters.
- You can index them with s[0], slice with s[start:end], and use negative indexing.

2. len(s)
- Returns the number of characters in the string.
Example:
len("abc") -> 3

3. String Slicing:
s[0:5] -> 'hello'
- Returns a portion of the string from index 0 to 4.

4. Reversing a string:
s[::-1]
- Step -1 reverses the string.

5. String Methods:
------------------
- s.upper() - Converts to uppercase.
- s.lower() - Converts to lowercase.
- s.title() - Capitalizes each word.
- s.strip() - Removes leading/trailing spaces.
- s.find(sub) - Returns index of first occurrence.
- s.replace("a", "b") - Replaces all 'a' with 'b'.

6. String Testing Methods:


--------------------------
- s.isalpha(): True if all characters are alphabets.
- s.isdigit(): True if all characters are digits.
- s.isalnum(): True if alphanumeric.
- s.isspace(): True if all characters are spaces.

7. Splitting and Joining:


-------------------------
s.split(" ") -> Splits by spaces.
" ".join(["A", "B"]) -> 'A B'
- Used for converting between list of words and string.

8. String Formatting:
---------------------
name = "Alice"
f"Hello, {name}!" -> 'Hello, Alice!'
- f-strings allow inline variable usage.
- "{} {}".format("A", "B") also works.

9. Count characters:
--------------------
from collections import Counter
Counter("banana") -> {'a': 3, 'n': 2, 'b': 1}

10. Palindrome Check:


---------------------
s == s[::-1] checks if the string is the same forward and backward.

11. Length of number:


---------------------
len(str(12345)) -> 5

====================================
SECTION 2: OBJECT ORIENTED PROGRAMMING (OOP)
====================================

1. Class and Object:


--------------------
class Person:
def __init__(self, name):
self.name = name

def greet(self):
print("Hi, I'm", self.name)

- Class is a blueprint. Object is an instance.


- __init__ is the constructor.

2. Inheritance:
---------------
class Student(Person):
def __init__(self, name, roll):
super().__init__(name)
self.roll = roll

- Student inherits from Person.


- super() calls parent constructor.

3. Encapsulation:
-----------------
class Bank:
def __init__(self):
self._protected = 100
self.__private = 200

- _protected: Internal use (convention).


- __private: Name mangled, not accessible directly.
4. Polymorphism:
----------------
class Dog:
def sound(self):
print("Bark")

class Cat:
def sound(self):
print("Meow")

- Same method name with different behavior.

5. Abstraction:
---------------
from abc import ABC, abstractmethod

class Shape(ABC):
@abstractmethod
def area(self): pass

- Abstract class cannot be instantiated.


- Must override abstract methods.

6. Static and Class Methods:


----------------------------
class Example:
@staticmethod
def static_func():
print("I don't need self")

@classmethod
def class_func(cls):
print("I know my class:", cls.__name__)

- Static methods don't need object or class reference.


- Class methods receive class as argument.

7. Dunder/Magic Methods:
------------------------
__str__, __init__, __len__, etc.

class Book:
def __str__(self):
return "Book object"

print(Book()) -> Uses __str__

8. Composition:
---------------
class Engine:
def start(self):
print("Engine start")

class Car:
def __init__(self):
self.engine = Engine()

def start(self):
self.engine.start()

- Using one class (Engine) inside another (Car).

====================================
EXAM PREP TIPS
====================================
- Memorize syntax for string slicing and formatting.
- Practice creating and inheriting classes.
- Know difference between static/class methods.
- Use OOP principles clearly in code.

Good luck with your CodeTantra exam!

You might also like