Self-taught python notes string update with little turtle...

After reading these notes, you can get started with Python.
Watch the video of Little Turtle on Station B, and organize your notes by the way.

Lesson 10 list
Lesson 11 Tuples
Lesson 13 format format

Lesson 12 strings

1. Definition and acquisition of strings

      In Python, a string can be defined with single or double quotes.

str1 = 'I love you' # str1 即为一个字符串

Like getting list elements, strings can also have string slices, as follows:
Insert picture description here
like tuples, insert strings:
Insert picture description here

2. Built-in methods of strings

Note: The content in the brackets is an optional parameter
      capital(): change the first character of the string to uppercase
      casefold(): change all characters to lowercase
      center(width): center the string and fill it with spaces To a new string of length width
      count(sub[,start[,end]]): the number of times sub appears in the string
      encode(encoding='utf-8', errors='strict'): specified by encoding The encoding format encodes the string.
      endswith(sub[, start[, end]]): Check whether the string ends with sub substring, if it is, return True, otherwise return False.
      expandtabs([tabsize=8]): Convert the tab symbol (\t) in the string to spaces. If no parameter is specified, the default number of spaces is tabsize=8.
      find(sub[, start[, end]]): Check whether sub is contained in the string, and if so, return the index value, otherwise return -1. The start and end parameters indicate the range and are optional.
      index(sub[, start[, end]]): Same as the find method, but an exception will be generated if sub is not in the string.
      isalnum(): Returns True if the string has at least one character and all characters are letters or numbers, otherwise it returns False.
      isalpha(): Returns True if the string has at least one character and all characters are letters, otherwise it returns False.
      isdecimal(): Returns True if the string contains only decimal digits, otherwise returns False.
      isdigit(): Returns True if the string contains only digits, otherwise returns False.
      islower(): If the string contains at least one case-sensitive character, and these characters are all lowercase, it returns True, otherwise it returns False.
      isnumeric(): If the string contains only numeric characters, it returns True, otherwise it returns False.
      isspace(): If the string contains only spaces, it returns True, otherwise it returns False.
      istitle(): If the string is titled (all words start with uppercase and the rest of the letters are lowercase), it returns True, otherwise it returns False.
      isupper(): If the string contains at least one case-sensitive character, and these characters are all uppercase, it returns True, otherwise it returns False.
      join(sub): Use a string as a separator to insert between all the characters in sub.
      ljust(width): Returns a left-justified string and pads it with spaces to a new string of length width.
      lower(): Convert all uppercase characters in the string to lowercase.
      lstrip(): remove all spaces on the left side of the string
      partition(sub): find the substring sub, divide the string into a 3-tuple (pre_sub, sub, fol_sub), if the string does not contain sub, return ('original String','','')
      replace(old, new[, count]): Replace the old substring in the string with the new substring. If count is specified, the replacement will not exceed count times.
      rfind(sub[, start[, end]]): Similar to the find() method, but search from the right.
      rindex(sub[, start[, end]]): Similar to the index() method, but starts from the right.
      rjust(width): Returns a right-justified string and pads it with spaces to a new string of length width.
      rpartition(sub): Similar to the partition() method, but search from the right.
      rstrip(): Remove the spaces at the end of the string.
      split(sep=None, maxsplit=-1): Without parameters, the default is to slice the string with spaces as the separator. If the maxsplit parameter is set, only maxsplit substrings will be separated, and the list of substrings after the slice will be returned. .
      splitlines(([keepends])): Whether to remove the newline character in the output result, the default is False, does not include the newline character; if it is True, the newline character is kept.
      startswith(prefix[, start[, end]]): Check whether the string starts with prefix, if yes, return True, otherwise return False.
      strip([chars]): Remove all spaces before and after the string.
      swapcase(): Reverse case in the string.
      title(): Returns a titled (all words start with uppercase, and the rest of the letters are lowercase).
      translate(table): Translate the characters in the string according to the rules of the table (which can be customized by str.maketrans('a','b')).
      upper(): Convert all lowercase characters in the string to uppercase.
      zfill(width): Returns a string of length width, the original string is right aligned, and the front is filled with 0.

2. Homework exercises for strings

Guess you like

Origin blog.csdn.net/A_Tu_daddy/article/details/105120611