First, define a string
in [+ ' This isa string ' # You can use single quotation mark/double quotation mark/three quotation mark to define string type in []: str = " This was a string " # in Python, there is no difference between single and double quotes in [] ' This isa string '
# Three quotes can be written as "' or" ", and three quotes can be used to make annotations in addition to string types
In ["hello\nworld" # Single and double quotes if you want to add \ n as a newline character in the cross-line definition - """ Hello # Three quotes can be entered directly on the next line .....: World" " "
Second, the index and slice of the string
in [ -]: a ='ABCDE'In [ -]: a[0] # The index value starts at 0 and represents the first character in a string out[ -]:'a'In [ -]: a[1] # takes the second character in a string out[ -]:'b'In [ in]: a[-1]#-1 means a countdown character, -2 represents the second-to-last character, and so on out[ in]:'e'In [ -]: a[0:2] # represents the first to third (not including the third) character of this range out[ -]:'AB'In [ to]: a[:2] # Zero can be omitted if the ellipsis means starting from the first index fetch out[ to]:'AB'In [ +]: a[1:] # This writes a second fetch from the last index out[ +]:'BCDE'In [ -]: a[-4:-2] # indicates the character out[the range from the fourth to the penultimate (excluding the penultimate one). -]:'BC'In [ the]: a[::1] # indicates that every other number is taken once out[ the]:'ABCDE'In [ *]: a[::2] # indicates that every two numbers are taken once out[ *]:'Ace'
Python String Type