PDA Assignments
  • Python For Data Analytics
    • 1.Python
      • 1.Python Documents
        • 1.Data Types
        • 2.Variables In Python
        • 3.Operators In Python
        • 4.User Input In Python
        • 5.TypeCasting In Python
        • 6.Strings In Python
        • 7.Conditional Statements In Python
        • 8.Branching using Conditional Statements and Loops in Python
        • 9.Lists In Python
        • 10.Sets In Python
        • 11.Tuples In Python
        • 12.Dictionary In Python
        • 13.Functions In Python
        • 14.File Handling In Python
        • 15.Numerical Computing with Python and Numpy
      • 2.Python Assignments
        • Data Type & Variables
        • Operators Assignment
        • User Input & Type Casting
        • Functions- Basic Assignments
        • String Assignments
          • String CheatSheet
        • Conditional Statements Assignments
        • Loops Assignments
        • List Assignments
          • List Cheatsheet
        • Set Assignments
          • Sets Cheatsheet
        • Dictionary Assignments
          • Dictionary Cheatsheet
        • Function Assignments
        • Functions used in Python
      • 3.Python Projects
        • Employee Management System
        • Hamming distance
        • Webscraping With Python
          • Introduction To Web Scraping
          • Importing Necessary Libraries
          • Basic Introduction To HTML
          • Introduction To BeautifulSoup
          • Flipkart Web Scraping
            • Scraping Step By Step
        • Retail Sales Analysis
        • Guess the Word Game
        • Data Collection Through APIs
        • To-Do List Manager
        • Atm-functionalities(nested if)
        • Distribution of Cards(List & Nested for)
        • Guess the Number Game
      • 4.Python + SQL Projects
        • Bookstore Management System
    • 2.Data Analytics
      • 1.Pandas
        • 1.Pandas Documents
          • 1.Introduction To Pandas
          • Reading and Loading Different Data
          • 2.Indexing and Slicing In Pandas
          • 3.Joining In Pandas
          • 4.Missing Values In Pandas
          • 5.Outliers In Pandas
          • 6.Aggregating Data
          • 7.DateTime In Pandas
          • 8.Validation In Pandas
          • 9.Fetching Data From SQL
          • 10. Automation In Pandas
          • 11.Matplotlib - Data Visualization
          • 12. Seaborn - Data Visualization
          • 13. Required Files
        • 3.Pandas Projects
          • Retail Sales Analysis
            • Retail Sales Step By Step
          • IMDB - Dataset Analysis - Basic
        • 2. Pandas Assignments
          • 1. Reading and Loading the Data
          • 2. Data frame Functions and Properties
          • 3. Series - Basic Operations
          • 4. Filtering in Pandas
          • 5. Advance Filtering
          • 6. Aggregate Functions & Groupby
          • 7. Pivot Tables
          • 8. Datetime
          • 9. String Functions
Powered by GitBook
On this page
  1. Python For Data Analytics
  2. 1.Python
  3. 2.Python Assignments
  4. String Assignments

String CheatSheet

Functions and Methods to be used in string data type

  1. Concatenation(+) : It joins a string with another string. str1='hi I am' str2='ayush' result= str1 + str2 # str3='hi I am ayush'

  2. Repitition( * ): It repeats a string. str='ayush' result= str * 3 # ayushayushayush

  3. Membership Operator:

    1. in : It checks the presence of a substring in a string. str1='hi I am Ayush' result= 'I' in str1 # True

    2. not in: It checks the absence of a subtring in a string. str1='hi I am Ayush' result='z' in str1 # True

  4. len(): It gives the length (number of characters) of the string. str1='hi i am Ayush' result= len(str1) # 13

  5. max(): It gives the maximum value character in a string. str1='Ayush' result=max(str1) # y

  6. min(): It gives the minimum value character in a string. str1='Ayush' result=min(str1) # A

  7. .lower(): It converts all the characters of a string to Lower case. str1='AYUSH' result=str1.lower() # ayush

  8. .upper(): It converts all the characters of a string to upper case. str1='ayush' result=str1.upper() # 'AYUSH'

  9. .capitalize(): It capitalizes the first character of the string only. str1='HI I AM AYUSH' result=str1.capitalize() # Hi i am ayush

  10. .title(): It capitalizes the first letter of each word in the string. str1='HI I AM AYUSH' result=str1.title() # Hi I Am Ayush

  11. .swapcase(): It converts all lowercase characters to uppercase and all uppercase characters to lowercase within the string. str1=' Hi I aM aYUsh' result= str1.swapcase() # hI i Am AyuSH

  12. .strip(): It removes all whitespaces from left and right side of the string. str1=' ayush ' result=str1.strip() # ayush

  13. .lstrip(): It removes all whitespaces from left side of the string only. str1=' ayush ' result= str1.lstrip() # ayush (includes spaces present on the right side)

  14. .rstrip():It removes all whitespaces from right side of the string only. str1=' ayush ' result=str1.rstrip() # ayush (includes spaces on the left side)

  15. .replace(old,new): Replaces all occurrences of the "old" substring with the "new" substring. str1='python is python and python is good.' result=str1.replace('Python','java') # java is java and java is good. .replace(old,new,count): Replaces old substring with the new substring depending upon the counting provide. str1='python is python and python is good' result=str1.replace('python','java',2) # java is java and python is good.

  16. .count('substring'): It gives the counting of a substring in the string. str1='python is python and python is good' result=str1.count('o') # 5 .count('substring',start,end): It gives the counting of a substring in the string. Counting starts from start index no. and ends at end index number. str1='python is python and python is good' result=str1.count('o',15,35) # 3

  17. .find('substring'): It gives the index number of the first appearance of the substring. str1='python is python and python is good' result=str1.find('y') # 1 .find('substring',start,end): It gives the index number of the first appearance of the substring starting from start point. str1='python is python' result=str1.find('y',2,15) # 11

  18. .rfind('substring'): It gives the index number of the first appearance of the substring from the right side. str1='python is python' result=str1.rfind('y') # 11

  19. .index('substring'): It gives the index number of the first appearance of the substring. str1='python is python and python is good' result=str1.find('y') # 1 .index('substring',start,end): It gives the index number of the first appearance of the substring starting from start point. str1='python is python' result=str1.find('y',2,15) # 11

  20. .rindex('substring'): It gives the index number of the first appearance of the substring from the right side. str1='python is python' result=str1.rfind('y') # 11

  21. .split(separator): It breaks a string into multiple substrings depending upon the separator. Default separater is 'space'. str1='python is a programming language' result=str1.split() #['python','is','a','programming','language'] split(separator,maxsplit): It breaks a string into multiple substrings depending upon the separator. Default separater is 'space'.Maxsplit defines the number of breaks you want of a string. str1='python is a programming language' result=str1.split(' ',2) #['python','is','a programming language']

  22. .rsplit(separator): It breaks a string into multiple substrings depending upon the separator. Default separater is 'space'. str1='python is a programming language' result=str1.rsplit() #['python','is','a','programming','language'] rsplit(separator,maxsplit): It breaks a string into multiple substrings depending upon the separator. Default separater is 'space'.Maxsplit defines the number of breaks you want of a string. str1='python is a programming language' result=str1.rsplit(' ',2) #['python is a','programming', 'language']

  23. .partition(): It breaks a string depending upon the separater but only once and also includes the separator in the output. str1='python is a programming language' result=str1.partition(' ') # ('python',' ' ,'is a programming language') .rpartition(): It breaks a string depending upon the separater but only once and also includes the separator in the output. str1='python is a programming language' result=str1.rpartition(' ') # ('python is a programming',' ', 'language')

  24. .islower(): It checks whether all the alphabets in a string are in lower case or not. str1='ayush' result=str1.islower() # True

  25. .isupper(): It checks whether all the alphabets in a string are in upper case or not. str1='AYUSH' result=str1.isiupper() # True

  26. .alpha(): It checks whether all the characters in a string are alphabet or not. str1='ayush' result=str1.alpha() # True

  27. .isnumeric(): It checks whether all the characters in a string are digit or not. str1='12345' result=str1.isnumeric() # True

  28. .isalnum(): It checks whether all the characters in a string are alphabet or digits only. str1='ayush12345' result=str1.isalnum # True

  29. .startswith(): It checks whether the string starts with a particular substring or not. str1='+91 1234567890' result=str1.startswith('+91') # True

  30. .endswith(): It checks whether the string ends with a particular substring or not. str1='myfile.txt' result=str1.endswith('txt') # True

PreviousString AssignmentsNextConditional Statements Assignments

Last updated 11 months ago