Python Strings/Lists/OOPS
Tags: lis, array, string, oops
Published: 2023-02-16 06:39:51


Every programming language has its methods for its data structures. Some of the data structures in Python include list, string, dictionary, and set.

## List methods

* append() - Append an element to the end of the list.
     ```
     l=[]
     l.append(5)
     print(l)
     # l=[5]
     ```
     <br/>
* clear() - Clears the list.
     ```
     l=[1,2]
     l.clear()
     print(l)
     # l=[]
     ```
<br/>
* copy() - Makes a copy of the list.
     ```
     l=[1,2,3]
     x=l.copy()
     print(x)
     # [1,2,3]
     ```
<br/>
* count() - Count the data that has been passed as an argument in the list.
     ```
     l=[1,2,3]
     x=l.count(1)
     print(x)
     # 1
     ```
<br/>
* extend() - This function extends the data of another list.
     ```
     l=[1,2,3]
     l1=[4,5,6]
     l=l.extend(l1)
     print(l)
     # [1,2,3,4,5,6]
     ```
<br/>
* index() - Returns the index of the passed element.
     ```
     l = [1,2,3]
     l=l.index(1)
     print(l)
     # 0
     ```
<br/>
* insert() - Takes two arguments, one being the position and another being the element. It will insert at the given position.
     ```
     l=[1,2,3]
     l.insert(0,0)
     print(l)
     #[0,1,2,3]
     ```
<br/>
* pop() - Removes the list's last element.
     ```
     l=[1,2,3]
     l.pop()
     print(l)
     # [1,2]
     ```
<br/>
* remove() - Removes the first matched element; the argument has to be passed.
     ```
     l=[1,2,3]
     l.remove(2)
     print(l)
     # [1,3]
     ```
<br/>
* reverse() - Reverse the order of the list.
     ```
     l= [1,2,3]
     print(l.reverse())
     # [3,2,1]
     ```
<br/>
* sort() - Sorts a list.
     ```
     l=[3,1,2]
     print(l.sort())
     # [1,2,3]
     ```
 <br/>
***

## String Methods

* capitalize() - Capitalizes the first character.
     ```
     s="string"
     k=s.capitalize()
     print(k)
     # String
     ```
<br/>
* casefold() - Lowers the case of a string.
     ```
     s = "String"
     casefold() = k
     print(k)
     # string
     ```
<br/>
* center() - Centres the string based on the argument.
     ```
     s="string"
     k=s.center(10)
     print(k)
     # string
     ```

* count() - Returns the number of occurrences of the value in the string.
     ```
     s="string"
     k=s.count(r)
     print(k)
     # 1
     ```
<br/>
* endswith() - Returns true if the string ends with the specified value.
     ```
     s="string"
     k=s.endswith(g)
     print(k)
     # True
     ```
<br/>
* find() - Searches the string for a specified value and returns the index of it; if not found, it returns -1.
     ```
     s="string"
     k=s.find(r)
     print(k)
     # -1
     ```
<br/>
* index() - Returns the index of a specified value after searching the string for it.
     ```
     s="string"
     k=s.index("r")
     print(k)
     # 2
     ```
<br/>
* isalnum() - If all characters in the string are alphanumeric, it will return true; otherwise, it will return false.
     ```
     s="string2"
     k=s.isalnum()
     print(k)
     # True
     ```
<br/>
* isalpha() - If all characters in the string are in the alphabet, it will return true; otherwise, it will return false.
     ```
     s="string"
     k=s.isalpha()
     print(k)
     # True
     ```
<br/>
* isascii() - Return true if all characters in the string are ASCII characters.
     ```
     s="string"
     k=s.isascii()
     print(k)
     # True
     ```
<br/>
* isdecimal() - This function returns True if all of the characters in the string are decimals.
     ```
     s="\u0033"
     k=s.isdecimal()
     print(k)
     # True
     ```
<br/>
* isdigit() - If all characters in the string are digits, it will return true; otherwise, it will return false.
     ```
     s="1234"
     k=s.isdigit()
     print(k)
     # True
     ```
<br/>
* islower() - If all characters in the string are lower case, it will return True; otherwise, it will return false.
     ```
     s="string"
     k=s.islower()
     print(k)
     # True
     ```
<br/>
* isnumeric() - If all characters in the string are numeric, it will return true; otherwise, it will return false.
     ```
     s="1234"
     k=s.isnumeric()
     print(k)
     # True
     ```
<br/>
* isprintable() - Returns true if the string contains all printable characters.
     ```
     s="string n adf"
     k=s.isprintable()
     print(k)
     # False
     ```
<br/>
* isspace() - Returns true if the string contains no whitespace; otherwise, it returns false.
     ```
     s="string"
     k=s.isspace()
     print(k)
     # False
     ```
<br/>
* istitle() -  If all of the words in the string begin with capital letters, istitle() returns true; otherwise, it returns false.
     ```
     "String the Word"
     k=s.istitle()
     print(k)
     # True
     ```
<br/>
* isupper() - True if all the characters are in upper case; otherwise, it will return false.
     ```
     s="STRING"
     k=s.isupper()
     print(k)
     # True
     ```
<br/>
* join() - Converts the elements of an iterable into a string.
     ```
     s=["s", "t", "r", "i", "n", "g"]
     k="".join(s)
     print(k)
     # string
     ```
<br/>
* ljust() - Returns a left-justified string.
     ```
     s="string"
     k=s.ljust(10)
     print(k)
     # denotes a string
     ```
<br/>
* lower() - Lowers the case of a string.
     ```
     s="StrInG"
     k=s.lower()
     print(k)
     # string
     ```
<br/>
* lstrip() - Removes the leftmost portion of the string.
     ```
     s=" string"
     k=s.lstrip()
     print(k)
     # string
     ```
<br/>
* replace() - Replace the matching values with the passed argument. It takes two arguments.
     ```
     s="string"
     k=s.replace("ing","")
     print(k)
     # str
     ```
<br/>
* rfind() - Searches the string for a given value and returns the last position it was found.
     ```
     s="stringr"
     k=s.rfind("r")
     print(k)
     # 6
     ```
<br/>
* rindex() - Searches the string for a specified value and returns the last position where it was found.
     ```
     s="stringr"
     k=s.rindex("r")
     print(k)
     # 6
     ```
<br/>
* rjust() - Returns a right-justified version of the string.
     ```
     s="string"
     k=s.rjust(10)
     print(k)
     # character string
     ```
<br/>
* rstrip() - Remove the appropriate portion of the string.
     ```
     s="string "
     k=s.rstrip()
     print(k)
     # string
     ```
<br/>
* split() - Splits the string based on the specified separator (the default is a space) and returns a list.
     ```
     string="string the word"
     k=s.split()
     print(k)
     # ["string", "the", "word"]
     ```

<br/>
* splitlines() -  splits each line and returns a list of the results.
     ```
     s="hello world string"
     k=s.splitlines()
     print(k)
     # ['string', 'hello world']
     ```
<br/>
* startswith() - This function looks for the letter in the string and returns True if it begins with the specified letter; otherwise, it returns False.
     ```
     s="string"
     k=s.startswith("s")
     print(k)
     # True
     ```
<br/>
* strip() - removes the string's trailing space.
     ```
     s=" string "
     k=s.strip()
     print(k)
     # string
     ```
<br/>
* swapcase() - swaps the cases so that the lower becomes the upper and the upper becomes the lower.
     ```
     string = "Word"
     k=s.swapcase()
     print(k)
     A wORD IS #STRING
     ```
<br/>
* title() - converts all of the word's first letters to uppercase.
     ```
     string = "word"
     k=s.title()
     print(k)
     String = Word
     ```
<br/>
* upper() - changes the string's case to upper case.
     ```
     s="string"
     k=s.upper()
     print(k)
     # STRING
     ```
<br/>
* zfill() -  prefix the number of zeros before the string.
     ```
     s="string"
     k=s.zfill(7)
     print(k)
     # 0string
     ```
<br/>

***

## OOPS

Object-oriented programming is a programming model that organises software design around data and objects rather than functions and logic. An object can be defined as a data field; it has unique attributes and behaviours.
<br/>

* OOPS structure:
  
  * Classes - It is like a combination of bundling data and functionality together. Each class instance can have attributes attached to it for maintaining its state it can also have methods to change its state.
  
  * Objects - An Object is an instance of a Class, an instance is a copy of the class with actual values. Python is object-oriented programming language  it mainly focus on functions.
  
  * Methods - A method is a function that belongs to  an particular object.    For example, list objects have methods called append, insert, remove and sort etc...
  
  * Attributes - Class attributes are class variables that are inherited by every object of a class. The value of class attributes remain the same for every new object.
  
<br/>

* Principles
<br/>
    * **Encapsulation**  - The binding up of methods and attributes, for example, a **class** is a combination of attributes and methods.
  

    * **Abstraction** - It's like a hiding of data; it can also help the developers more easily make additional changes over time.
         ```
       Import ABC from ABC
       class ClassName(ABC)
       # We can make the actual class abstract by inheriting the ABC class.
        ```
    * **Inheritance** - It's a concept of reusing the same code to avoid redundancy. This method will force the developer to reduce development time and also ensure high accuracy. There are multiple inheritances.

        - Single - derived from a single base or parent.
  
            ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fbhr6b43vbhq7bzqyyq7.png)
   
        - Multiple - derived from multiple base classes.
   
            ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eiuieox2ogyom0ted897.png)
        - Multilevel - deriving a class from a derived class that has been derived from a base class. 
  
          
            ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hm2yg9gdlv3zzp52pder.png)

        - Hierarchical: there are multiple derived classes from a single parent class.
     
            ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ro447m9lpxhe8xd0lb46.png)

    * **polymorphism** - The term "polymorphism" refers to the ability to use the same method as the parent class.
       ```
         add(x, y, z) = 0:
         x + y + z = return
       
         print(add(2, 3))
         # 5
         print(addition(2, 3, 4))
         # 9
       ```

***

## References
- https://www.w3schools.com/python/python_ref_string.asp
- https://www.w3schools.com/python/python_ref_list.asp
- https://www.dotnettricks.com/learn/oops/understanding-inheritance-and-different-types-of-inheritance