String - Python

  • Strings are immutable
  • varS = "value" => Declare a string
    • varS = 'value'
  • varS = '''value''' => Write multi-line string or Use \ at the end of every line
    • varS = """value"""
  • len(varS) => Returns length of string
  • Indexing/Slicing
    • varS[N] => Returns character at (N+1)th occurrence
    • varS[N1:N2] => Returns character from N1 to (N2-1)
    • varS[:N2] => Returns character from 0 to (N2-1)
    • varS[N1:] => Returns character from N1 to (length of string - 1)
    • varS[N1:-N2] => Returns character from N1 to (length of string - N2 - 1)
    • varS[N1:-N2] => Returns character from N1 to (length of string - N2 - 1)
    • varS[-N1:-N2] => Returns character from (length of string - N1) to (length of string - N2 - 1)
    • varS[N1:N2:N3] => Returns characters from N1 to N2-1 in N3 order
  • varS.upper() => Returns string in upper case
  • varS.lower() => Returns string in lower case
  • varS.rstrip("character") => Returns string with trailing given characters removed
  • varS.replace("varS1", "varS2") => Returns string with all occurrence of string1 replaced with string2
  • varS.split('character') => Returns list of values of string separated by given character
  • varS.capitalize() => Returns string with first character turned uppercase and rest lowercase
  • varS.center(N) => Aligns the string to the center by filling remaining with spaces
  • varS.count("varS1") => Returns counts of number of times the given value occurs
  • varS.find("varS1") => Returns index of first occurrence of given value, -1 if not present
  • varS.index("varS1") => Like find method but raises an exception if value is not found
  • varS.swapcase() => Returns string with cases of characters swapped
  • varS.title() => Returns string with first character of each word capitalized
  • varS.endswith("varS1") => Returns boolean True if string ends with given value
    • varS.endswith("varS1", N1, N2) => Checks after slicing string
  • varS.startswith("varS1") => Returns boolean True if string starts with given value
  • varS.isalnum() => Returns boolean True if string only contains "A-Z", "a-z", "0-9"
  • varS.isalpha() => Returns boolean True if string only contains "A-Z", "a-z"
  • varS.isnumeric() => Returns boolean True if string only contains "0-9"
  • varS.islower() => Returns boolean True if all characters are in lower case
  • varS.isupper() => Returns boolean True if all characters are in upper case
  • varS.isprintable() => Returns boolean True if all the values within the given string are printable
  • varS.isspace() => Returns boolean True if all characters are spaces
  • varS.istitle() => Returns boolean True if first character of each word is a capital
  • Concatenation
    • varS = "value1" + "value2" => Returns joints of both string values
  • varS1.join(varS2) => Joins string2 after string1
  • Checks if value is present in the string
        if "val" in "value":
            # statements
        else:
            # statements
    
  • Loop Characters
        for character in varS:
            print(character)
    
  • Format
    • varS.format(value1, value2) => Returns string with values of {} replaced in the string with given values
      • varS.format(value1, value2) => {0} inside string is replaced with value1 and {1} is replaced with value2
      • varS.format(varName = value) => "varName" inside string is replaced with given value
      • varS = "{varName:.Nf}" => value of "varName" when replaces will be formated to "N" decimal places
    • varS = f"{varName1} {varName2}" => Use variables inside strings
      • varS = f"{{varName1}}" => Prints "{varName1}"
      • varS = f"{varName:.Nf}" => Format value
Share: