String data type

  • A String is a sequence of one or more characters enclosed with in single apostrophe ' ' or double apostrophe " "

  • In python we don't have CHAR data type

  • A python string may contins any characters

  • Example:

    >>>username="David"
    >>>email='dwalker@mail.com'

    each character of a python string contains +ve and -ve index values as shown


  1. String slicing
  2. String handling method

String slicing

retriving some portion of a given string

  • Slice operator:- [start:end+1:step]
  • The default values are
    • start --- 0
      end+1 --- length of the string
      step  --- 1
  • If the step is +ve number then second part is end+1
  • If the step is -ve number then second part is end-1
      • example :-
        >>>x = "COMPUTER"
    x="COMPUTER" print(x[0:4]) #prints COMP print(x[0:8:2]) #prints CMUE print(x[-3:-8:-1]) #prints TUPMO print(x[4:]) #prints UTER print(x[:-3]) #prints COMPU print(x[-2:]) #prints ER
    Just press 'Run'.


    String handling method

    There are many string handling following are most widely used string handling methods

    1. capitalize(): Converts the first character to upper case
    2. >>>user="python"
      >>>print(user.capitalize())
      Python

    3. lower(): Converts string into lower case
    4. >>>user="PYTHON
      >>>print(user.lower())
      python

    5. upper(): Converts string into upper case
    6. >>>user="python"
      >>>print(user.upper())
      PYTHON

    7. find(): returns index of the first occurrence of a substring in a main string.
          if substring not found then returns -1.
    8. >>>mystr="This is python"
      >>>print(mystr.find('is'))
      2
      >>>print(mystr.find('Is'))
      -1 #python is case sensitive
      >>>print(mystr.find('was'))
      -1

    9. count(): use this method to count how many times a sub string has occured in the main string
    10. >>>mystr="Python is simple,Python is awesome,python is easy'
      >>>print(mystr.count('Python'))
      2
      >>>print(mystr.count('python'))
      1

    11. replace(): use this method to replace one substring with another substring.
    12. >>>mystr="This is core python"
      >>>print(mystr.replace('core','advance'))
      This is advance python

    13. split(): use this method to divide one into a list of multiple strings at a spliting characters.
          if the spliting character is not provided then by default it will split at white-space.
    14. >>>mystr="python is simple;python is awesome"
      >>>print(mystr.split(';'))
      ['python is simple' ; 'python is awesome']
      >>>mystr="python is simple'
      >>>print(mystr.split())
      ['python' , 'is' , 'simple']

    15. join(): use this method to join a list of strings into a single string with a given string
    16. >>>lst=['python' , 'is' ,'simple' ]
      >>>print('->'.join(lst))
      python->is->simple
      >>>print(''.join(lst))
      python is simple

    17. startswith(): use this method to check weather a main string starts with a given substring or not.
      If yes then returns True otherwise returns False
    18. >>>username="David Walker"
      print(username.startswith("david')
      False
      print(username.startswith("David")
      True

    19. endswith(): use this method to cheack weather a main string ends with a given substring or not.
      If yes then returns True otherwise returns False
    20. >>>username="David Walker"
      print(username.endswith("walker")
      False
      print(username.endswith("Walker")
      True

    21. strip(): use this method for removing white-space at before start of the value or end of the value.
    22. >>>mystr="   David Walker   "
      >>>print(mystr)
      #white-space at start & end of the value
      David Walker
      >>>print(mystr.strip())
      David Walker
        Note: To remove white-space only form left side then call lstrip() and to remove only from right side then call rstrip()

    23. isalpha: Returns True if all characters in the string are in the alphabet
    24. >>>mystr="CompanyX"
      >>>print(mystr.isapha())
      True

    25. isdigit(): Returns True if all characters in the string are digits
    26. >>>dig=20334
      >>>print(dig.isdigit())
      True