Basic string operations in Python

Python has built-in functions to do all the basic string operations.
Let us go through each one with simple examples.

  1. capitalize()

    Converts first character of the string to upper case.

    str1 = 'this is a test string. another sentence.'
    print(str1.capitalize())
    
    Output:
      This is a test string. another sentence.
  2. casefold()

    Converts string to lower case.

    str1 = 'This IS a test stRing'
    print(str1.casefold())
    
    Output:
      this is a test string
  3. center()

    Adds specified character around string. Default is space.

    str1 = "Test"
    print(str1.center(10))
    str2 = "String"
    print(str2.center(10, 'X'))
    
    Output:
            Test
      XXStringXX
  4. encode()

    Encodes a string in specified format. Default is UTF-8 which has values for almost all characters. We can specify additional formatting type using encoding attribute. Formats like ASCII does not have equivalent for all characters and will throw error.

    To avoid errors, we can provide errors attribute, which mentions what to do when an error occurs. Sample below.

    str1 = "Test Encode æÅ¢╣ò"
    print(str1.encode())
    str2 = "Test Encode"
    print(str2.encode(encoding='ascii'))
    
    #Throws error because ascii code not available for given characters
    #print(str1.encode(encoding='ascii'))
    #To ignore errors
    print(str1.encode(encoding='ascii', errors='ignore'))
    #Replace error characters with ?
    print(str1.encode(encoding='ascii', errors='replace'))
    
    Output:
      b'Test Encode \xc3\xa6\xc3\x85\xc2\xa2\xe2\x95\xa3\xc3\xb2'
      b'Test Encode'
      b'Test Encode '
      b'Test Encode ?????'
  5. endswith()

    True if the string ends with the specified character(s). False otherwise.

    str1 = "Test String"
    print(str1.endswith('ing'))
    
    Output:
      True
  6. expandtabs()

    Set tab size to specified value. Default is 8.

    str1 = "Test\tString\tTest"
    print(str1.expandtabs(2))
    print(str1.expandtabs(10))
    
    Output:
      Test  String  Test
      Test          String          Test
  7. find()

    Finds the first occurrence of specified character(s) within the string and returns position. Optional parameters start and end to limit the search.

    Almost similar to index method. Difference is that index method raises exception, while find returns -1.

    str1 = "Test String Test"
    print(str1.find('es'))
    print(str1.find('es', 5))
    print(str1.find('es', 5, 10))
    
    Output:
      1
      13   -1
  8. format()

    Formats a specified value in specified format and combines to the string.

    s1 = "String with 2 values: {0} and {1}".format("One", 2)
    s2 = "String with 2 values: {first} and {second}".format(first ="One", second =2)
    print (s1)
    print (s2)
    marksPercent = "Marks Percent is {:.1%}"
    print(marksPercent.format(0.7522))
    
    Output:
    String with 2 values: One and 2
    String with 2 values: One and 2
    Marks Percent is 75.2%

    Refer Python String Formatting for more on formatting strings in Python.

  9. index()

    Finds the first occurrence of specified character(s) within the string and returns position. Optional parameters start and end to limit the search.

    Almost similar to find method. Difference is that index method raises exception, while find returns -1.

    str1 = "Test String Test"
    print(str1.index('es'))
    print(str1.index('es', 5))
    print(str1.index('es', 5, 15))
    #Throws exception
    #print(str1.index('ss'))
    
    Output:
    Output:
      1
      13   13
  10. isalnum()

    Checks if the entire string is alphanumeric.

    str1 = "TestString"
    print(str1.isalnum())
    
    str1 = "Test String"
    print(str1.isalnum())
    
    str1 = "Test1String"
    print(str1.isalnum())
    
    Output:
      True
      False
      True
  11. isalpha()

    Checks if the entire string contains alphabet only.

    str1 = "TestString"
    print(str1.isalpha())
    
    str1 = "Test String"
    print(str1.isalpha())
    
    str1 = "Test1String"
    print(str1.isalpha())
    
    Output:
      True
      False
      False
  12. isascii()

    Checks if string has non ASCII characters.

    str1 = "Test String"
    print(str1.isascii())
    
    str1 = "Test Stringæææææ"
    print(str1.isascii())
    
    Output:
      True
      False
  13. isdecimal()

    Checks if all characters are decimals in number or unicode format.

    str1 = "1000"
    print(str1.isdecimal())
    
    str1 = "100.45"
    print(str1.isdecimal())
    
    str1 = "\u0035"
    print(str1.isdecimal())
    
    Output:
      True
      False
      True
  14. isdigit()

    Checks if all characters are numbers.

    str1 = "1000"
    print(str1.isdigit())
    
    str1 = "100.45"
    print(str1.isdigit())
    
    str1 = "\u0035"
    print(str1.isdigit())
    
    Output:
      True
      False
      True
  15. isidentifier()

    Identifiers are any names used in Python and returns true most of the time.

    str1 = "Test"
    print(str1.isidentifier())
    
    str1 = "while"
    print(str1.isidentifier())
    
    Output:
      True
      True
  16. islower()

    Checks if entire string is in lower case.

    str1 = "test"
    print(str1.islower())
    
    str1 = "Test"
    print(str1.islower())
    
    Output:
      True
      False
  17. isnumeric()

    Checks if string has digits only.

    str1 = "test"
    print(str1.isnumeric())
    
    str1 = "123"
    print(str1.isnumeric())
    
    str1 = "123.23"
    print(str1.isnumeric())
    
    Output:
      False
      True
      False
  18. isspace()

    Checks if provided string has spaces only.

    str1 = "test"
    print(str1.isspace())
    
    str1 = "   "
    print(str1.isspace())
    
    str1 = "  a  "
    print(str1.isspace())
    
    Output:
      False
      True
      False
  19. istitle()

    To return true, first character of each word in the string should be in Upper case and the rest in Lower case.

    str1 = "test"
    print(str1.istitle())
    
    str1 = "Test"
    print(str1.istitle())
    
    str1 = "Test title"
    print(str1.istitle())
    
    str1 = "Test Title"
    print(str1.istitle())
    
    str1 = "TEST Title"
    print(str1.istitle())
    
    Output:
      False
      True
      False
      True
      False
  20. isupper()

    Checks if all characters in upper case.

    str1 = "test"
    print(str1.isupper())
    
    str1 = "TEST"
    print(str1.isupper())
    
    Output:
      False
      True
  21. join()

    Join items in a list, tuple, set or dictionary using a mentioned separator.

    list1 = ["a", "b"]
    print(''.join(list1))
    
    
    print('111'.join(list1))
    
    Output:
      ab
      a111b
  22. lower()

    Returns string in lower case.

    str1 = 'Test'
    print(str1.lower())
    
    Output:
      test
  23. lstrip()

    Strip white spaces from left side.

    str1 = ' Test '
    print(str1.lstrip())
    
    Output:
      Test 
  24. maketrans()

    Creates a translation mapping table to replace a set of characters with new ones. Should be used with translate method to replace the text.

    First parameter mentions characters to replace. Second parameter mentions new set of characters. Should be equal in length with first parameter. Third parameter is option. Contains characters to be removed.

    str1 = 'Foo Bar'
    translation1 = str1.maketrans('oa', 'yx')
    print(translation1)
    print(str1.translate(translation1))
    
    str1 = 'Foo Bar'
    translation2 = str1.maketrans('oa', 'yx', 'FB')
    print(str1.translate(translation2))
    
    Output:
      {111: 97, 120: 121}
      Fyy Bxr
      yy xr
  25. partition()

    Returns a 3 part tuple based on the partition string provided. If no match, tuple will contain, complete string as first item and two empty strings.

    str1 = 'Test partition string'
    print(str1.partition('partition'))
    print(str1.partition('string'))
    print(str1.partition('different'))
    
    Output:
      ('Test ', 'partition', ' string')
      ('Test partition ', 'string', '')
      ('Test partition string', '', '')
  26. removeprefix()

    Remove a set of characters, if string starts with this.

    str1 = 'TestString'
    print(str1.removeprefix('Test'))
    
    Output:
      String
  27. removesuffix()

    Remove a set of characters, if string ends with this.

    str1 = 'TestString'
    print(str1.removesuffix('String'))
    
    Output:
      Test
  28. replace()

    Replace a particular character or character set with another set. By default, this replaces all occurrence. We can add a third optional parameter to mention how many occurrences to replace.

    str1 = 'Test string'
    print(str1.replace('r', 'x'))
    print(str1.replace('s', 'y', 1))
    
    Output:
      Test stxing
      Teyt string
  29. Reversing Strings

    To reverse string, do it like slicing and provide -1 as the third item.

    str1 = "Test String"
    print(str1[::-1])
    
    Output:
      gnirtS tseT
  30. rfind()

    Finds the last occurrence of specified character(s) within the string and returns position. Optional parameters start and end to limit the search.

    Almost similar to rindex method. Difference is that rindex method raises exception, while rfind returns -1.

    str1 = "Test String Test"
    print(str1.rfind('es'))
    print(str1.rfind('es', 5))
    print(str1.rfind('es', 5, 10))
    
    Output:
      13
      13   -1
  31. rindex()

    Finds the last occurrence of specified character(s) within the string and returns position. Optional parameters start and end to limit the search.

    Almost similar to rfind method. Difference is that rindex method raises exception, while rfind returns -1.

    str1 = "Test String Test"
    print(str1.rindex('es'))
    print(str1.rindex('es', 5))
    print(str1.rindex('es', 5, 15))
    #Throws exception
    #print(str1.rindex('ss'))
    
    Output:
    Output:
      13
      13   13
  32. rpartition()

    Returns a 3 part tuple based on the partition string provided. If no match, tuple will contain, complete string as last item and two empty strings. Difference between partition and rpartition is this empty string position, for no match.

    str1 = 'Test partition string'
    print(str1.rpartition('partition'))
    print(str1.rpartition('string'))
    print(str1.rpartition('different'))
    
    Output:
      ('Test ', 'partition', ' string')
      ('Test partition ', 'string', '')
      ('', '', 'Test partition string')
  33. rsplit()

    Splits string based on the specified character. An optional parameter is there to specify maximum number of splits. For rsplit, splitting starts from right side and this is relevant if maximum number of splits are specified. For split, splitting starts from left side.

    str1 = 'Test#split#string'
    print(str1.rsplit('#'))
    print(str1.rsplit('##'))
    print(str1.rsplit('#', 1))
    
    Output:
      ['Test', 'split', 'string']
      ['Test#split#string']
      ['Test#split', 'string']
  34. rstrip()

    Strip white spaces from right side.

    str1 = '  Test  '
    print(str1.rstrip())
    
    Output:
        Test
  35. Slicing Strings

    To slice string, mention start and end or start or end.

    str1 = "Test String"
    print(str1[3:8])
    print(str1[3:])
    print(str1[:8])
    
    Output:
      t Str
      t String
      Test Str
  36. split()

    Splits string based on the specified character. An optional parameter is there to specify maximum number of splits. For split, splitting starts from left side and this is relevant if maximum number of splits are specified. For rsplit, splitting starts from right side.

    str1 = 'Test#split#string'
    print(str1.split('#'))
    print(str1.split('##'))
    print(str1.split('#', 1))
    
    Output:
      ['Test', 'split', 'string']
      ['Test#split#string']
      ['Test', 'split#string']
  37. splitlines()

    Split string at line breaks.

    str1 = 'Test\nString\nTest'
    print(str1.splitlines())
    
    Output:
      ['Test', 'String', 'Test']
  38. startswith()

    True if the string starts with the specified character(s). False otherwise.

    str1 = "Test String"
    print(str1.startswith('Te'))
    
    Output:
      True
  39. strip()

    Strip white spaces from both sides.

    str1 = '  Test  '
    print(str1.strip())
    
    Output:
      Test
  40. swapcase()

    Swaps cases of the given string.

    str1 = 'TestString'
    print(str1.swapcase())
    
    Output:
      tESTsTRING
  41. title()

    Converts first case of each word to upper case.

    str1 = 'test string'
    print(str1.title())
    
    Output:
      Test String
  42. translate()

    translate method is used to replace or remove a set of characters mentioned in a mapping table or dictionary. Usually used in conjunction with maketrans method to create a mapping table.

    str1 = 'Foo Bar'
    translation1 = str1.maketrans('oa', 'yx')
    print(translation1)
    print(str1.translate(translation1))
    
    str1 = 'Foo Bar'
    translation2 = str1.maketrans('oa', 'yx', 'FB')
    print(str1.translate(translation2))
    
    Output:
      {111: 97, 120: 121}
      Fyy Bxr
      yy xr
  43. upper()

    Converts string to upper case.

    str1 = 'test string'
    print(str1.upper())
    
    Output:
      TEST STRING
  44. zfill()

    Appends zeroes at the beginning so that the string length becomes specified value. If length already greater then the specified number, no zeroes appended.

    str1 = 'Test'
    print(str1.zfill(7))
    
    str1 = 'TestString'
    print(str1.zfill(7))
    
    Output:
      000Test
      TestString
Absolute Code Works - Python Topics