PYTHON SUBSTRINGS Higher Computing Science
SUBSTRINGS • A substring is a string of characters that are contained within another string. For example: • ‘Bits’ is a substring of the string ‘Bits and bytes’ • ‘Bytes is a substring of the string ‘Bits and bytes’ • ‘its and by’ is a substring of the string ‘Bits and bytes’
PYTHON SUBSTRINGS
EXAMPLE A program is required to generate a customer code. It will ask for the customer forename, surname and their year of birth. The program will then create a 6 digit customer code using: • First 2 letters of forename • Last 2 digits of year of birth • First 2 letters of surname john smith 1995 would have the customer code: jo95sm
SOLUTION
SOLUTION

Python Substrings

  • 1.
  • 2.
    SUBSTRINGS • A substringis a string of characters that are contained within another string. For example: • ‘Bits’ is a substring of the string ‘Bits and bytes’ • ‘Bytes is a substring of the string ‘Bits and bytes’ • ‘its and by’ is a substring of the string ‘Bits and bytes’
  • 3.
  • 4.
    EXAMPLE A program isrequired to generate a customer code. It will ask for the customer forename, surname and their year of birth. The program will then create a 6 digit customer code using: • First 2 letters of forename • Last 2 digits of year of birth • First 2 letters of surname john smith 1995 would have the customer code: jo95sm
  • 5.
  • 6.