DEV Community

viyashdoss
viyashdoss

Posted on

Python Strings/Lists/OOPS

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]
    
  • clear() - Clears the list.

     l=[1,2]
     l.clear()
     print(l)
     # l=[]
    
  • copy() - Makes a copy of the list.

     l=[1,2,3]
     x=l.copy()
     print(x)
     # [1,2,3]
    
  • 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
    
  • 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]
    
  • index() - Returns the index of the passed element.

     l = [1,2,3]
     l=l.index(1)
     print(l)
     # 0
    
  • 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]
    
  • pop() - Removes the list's last element.

     l=[1,2,3]
     l.pop()
     print(l)
     # [1,2]
    
  • remove() - Removes the first matched element; the argument has to be passed.

     l=[1,2,3]
     l.remove(2)
     print(l)
     # [1,3]
    
  • reverse() - Reverse the order of the list.

     l= [1,2,3]
     print(l.reverse())
     # [3,2,1]
    
  • sort() - Sorts a list.

     l=[3,1,2]
     print(l.sort())
     # [1,2,3]
    

String Methods

  • capitalize() - Capitalizes the first character.

     s="string"
     k=s.capitalize()
     print(k)
     # String
    
  • casefold() - Lowers the case of a string.

     s = "String"
     casefold() = k
     print(k)
     # string
    
  • 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
    
  • endswith() - Returns true if the string ends with the specified value.

     s="string"
     k=s.endswith(g)
     print(k)
     # True
    
  • 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
    
  • index() - Returns the index of a specified value after searching the string for it.

     s="string"
     k=s.index("r")
     print(k)
     # 2
    
  • 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
    
  • 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
    
  • isascii() - Return true if all characters in the string are ASCII characters.

     s="string"
     k=s.isascii()
     print(k)
     # True
    
  • isdecimal() - This function returns True if all of the characters in the string are decimals.

     s="\u0033"
     k=s.isdecimal()
     print(k)
     # True
    
  • 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
    
  • 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
    
  • 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
    
  • isprintable() - Returns true if the string contains all printable characters.

     s="string n adf"
     k=s.isprintable()
     print(k)
     # False
    
  • isspace() - Returns true if the string contains no whitespace; otherwise, it returns false.

     s="string"
     k=s.isspace()
     print(k)
     # False
    
  • 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
    
  • isupper() - True if all the characters are in upper case; otherwise, it will return false.

     s="STRING"
     k=s.isupper()
     print(k)
     # True
    
  • join() - Converts the elements of an iterable into a string.

     s=["s", "t", "r", "i", "n", "g"]
     k="".join(s)
     print(k)
     # string
    
  • ljust() - Returns a left-justified string.

     s="string"
     k=s.ljust(10)
     print(k)
     # denotes a string
    
  • lower() - Lowers the case of a string.

     s="StrInG"
     k=s.lower()
     print(k)
     # string
    
  • lstrip() - Removes the leftmost portion of the string.

     s=" string"
     k=s.lstrip()
     print(k)
     # string
    
  • replace() - Replace the matching values with the passed argument. It takes two arguments.

     s="string"
     k=s.replace("ing","")
     print(k)
     # str
    
  • 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
    
  • 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
    
  • rjust() - Returns a right-justified version of the string.

     s="string"
     k=s.rjust(10)
     print(k)
     # character string
    
  • rstrip() - Remove the appropriate portion of the string.

     s="string "
     k=s.rstrip()
     print(k)
     # string
    
  • 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"]
    
  • splitlines() - splits each line and returns a list of the results.

     s="hello world string"
     k=s.splitlines()
     print(k)
     # ['string', 'hello world']
    
  • 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
    
  • strip() - removes the string's trailing space.

     s=" string "
     k=s.strip()
     print(k)
     # string
    
  • 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
    
  • title() - converts all of the word's first letters to uppercase.

     string = "word"
     k=s.title()
     print(k)
     String = Word
    
  • upper() - changes the string's case to upper case.

     s="string"
     k=s.upper()
     print(k)
     # STRING
    
  • zfill() - prefix the number of zeros before the string.

     s="string"
     k=s.zfill(7)
     print(k)
     # 0string
    

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.

  • 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.
  • Principles

    • 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

      • Multiple - derived from multiple base classes.

        Image description

      • Multilevel - deriving a class from a derived class that has been derived from a base class.

        Image description

      • Hierarchical: there are multiple derived classes from a single parent class.

        Image description

    • 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

Top comments (0)