• String method

    • capitalize()

      Capitalize the first letter.

    • count(sub[, start[, end]])

      Find the number of occurrences of sub in the range from start to end.

    • len()

      Returns the length of the string

    • endswith**(suffix[, start[, end]])

      Check whether the string is suffixed by a substring, with start indicating where the match starts and end indicating where the match stops.

    • startswith**(prefix[, start[, end]])

      Check if the string is prefixed by a substring, where start indicates the position at which the match starts and end indicates the position at which the match stops.

    • find**(sub[, start[, end]])

      Select * from string where substring sub appears for the first time, return -1, start sets the start index, end sets the end index.

    • rfind(sub[, start[, end]])

      Select * from string where substring sub was last found, return -1, start sets the start index, end sets the end index.

    • index**(sub[, start[, end]])

      Same as find, but raises ValueError if it cannot be found.

    • rindex(sub[, start[, end]])

      Same as the rfind method, but raises ValueError if it cannot be found.

    • sep.join(str)

      Split each character of STR with sep as delimiter and return the new delimited string.

    • lower**()

      Returns all characters in the original string to lowercase.

    • upper**()

      Returns all characters in the original string in uppercase.

    • isalnum()

      Return True if a string has at least one character and all characters are letters or numbers, False otherwise.

    • isalpha()

      Return True if a character has at least one character and all characters are letters, False otherwise.

    • isdigit()

      Returns True if a character has at least one character and all characters are digits, False otherwise.

    • isspace()

      Return True if a string has at least one character and all consists of whitespace characters, False otherwise.

    • istitle()

      Returns True if a string has at least one character and each word begins with uppercase followed by lowercase, False otherwise.

    • isupper()

      Return True if a string contains at least one character and all letters are uppercase, False otherwise.

    • swapcase()

      Converts uppercase letters to lowercase letters and lowercase letters to uppercase letters.

    • eplace**(old, new[, count])

      Return to replace old characters with new characters in the original string, no more than count(default all replacements) times.

    • split**(sep=None[, maxsplit=-1])

      Split string by sep returns a list object, if maxsplit is specified, split maxsplit times from the left.

    • rsplit(sep=None, maxsplit=-1)

      Split string by sep returns a list object, if maxsplit is specified, split maxsplit times from the right.

    • strip**()

      Deletes before and after whitespace or specified characters

    • lstrip([chars])

      Truncate the space or specified character to the left of the string.

    • rstrip([chars])

      Truncate the space or specified character at the 4 edge of the string.

    • casefold()

      Convert uppercase letters to lowercase letters.

    • center(width[, fillchar])

      Specifies the string to display width, with any gaps filled with fillchar(default space) characters and the original string centered.

    • ljust(width[, fillchar])

      Specifies the string to display width, with any gaps filled with fillchar(default space) characters, and the original string to display to the left.

    • rjust(width[, fillchar])

      Specifies the string to display width, with any gaps filled with fillchar(default space) characters, and the original string to display to the right.

    • zfill(width)

      Specifies that the string displays width, and any gaps are filled with 0 on the left.

    • max()

      Returns the character with the highest value in the string

    • min()

      Returns the character with the smallest value in the string

    • str.encode(encoding=”utf-8″, errors=”strict”)

      Converts a string to its byte-encoded object in the specified encoding

    • expandtabs(tabsize=8)

      Replace \t with Spaces in the string. By default, one \t is replaced with eight Spaces.

Code Exercise 1

A common function for stringslen() - Returns the length of the string (the number of characters in the string) mystr=""
print(len(mystr))    0Upper () - All characters of the string are uppercase mystr="hello"
print(mystr.upper())  
print(mystr)    #upper() does not change the original stringLower () - All characters of the string are lowercase mystr="HE" 
print(mystr.lower())  
print(mystr) capitalize() - Capitalize the first character of the string mystr="java"
print(mystr.capitalize()) 
print(mystr) find() - Checks if the string contains a given part of the string, and returns the index if it does (>=)0) otherwise return -1
mystr = "hello java hello"
print(mystr.find("hello")) rfind() - same as find(), start from the rightprint(mystr.rfind("hello")) index() - Like find(), except that an error is thrown if the given partial string is not contained (find() returns -1)print(mystr.index("python")) split() - Splits the entire string into multiple strings with the given separator, the default separator being the space mystr="hello+java"
print(mystr.split("+")) replace() - replaces the matched content with the given partial string mystr ="3 + 5 + 7 + 9"
print(mystr.replace("+"."-")) strip() - Removes the given character before and after the string. By default, the whitespace mystr = is removed" hello python----- \n "
mystr = mystr.strip()
mystr = mystr.strip("-")
print(mystr) count() - Counts the number of occurrences of a given string in a string mystr ="sfghjavajkl; jjavakl; l"
print(mystr.count("java")) endswith() - checks if the string endswith a given partial string startswith() - checks if the string begins with a given partial string mystr ="hello.py" 
print(mystr.endswith(".py"))  
Copy the code

String = “Python is good” string = “Python is good”

  1. string[1:20]
  2. string[20]
  3. string[3:-4]
  4. string[-10:-3]
  5. string.lower()
  6. string.replace(“o”, “0”)
  7. string.startswith(‘python’)
  8. string.split()
  9. len(string)
  10. string[30]
  11. string.replace(” “, ”)

Code Exercise 2

The join() string acts as a concatenator to join a given sequence of characters into a new stringprint(The '-'.join('hello'))  
# Max () returns the character with the maximum value
print(max('hellojavaw')) 
# min() returns the smallest character
print(min('hellojava')) 

# format()    
print("My name is {name}, I am {age}".format(name='zhangsan',age=20)) 
Copy the code