python strings, string operations, comparison, indexing, slicing, etc.

One:
String: str
Function: used to record text information,
literal representation method: the parts enclosed in quotation marks are all strings.
'' Single quotation mark
"" Double quotation mark
''' Triple quotation mark
"""
The difference between
single quotation mark and double quotation mark: the double quotation
mark of the single quotation mark is not the terminator The single quotation mark inside the double quotation mark is not the terminator
triple quotation mark string The role:
triple quotes can contain single quotes and double quotes
. The newline of the triple-quoted string will be automatically converted to a newline '\n'


Two: Use escape sequences to represent special characters . In
literal values, use subcharacters backslash \
followed by some characters to represent a special character
such as: \' represents a single quote
\" represents a double quote
\n represents a newline character
string Medium backslash character table:
\' single quote
\" double quote
\ backslash
\n newline
\r return cursor to start of line
\f form feed
\t horizontal tab
\v vertical tab
\b backspace
\0 String with character value zero
\xXX XX 2-digit hexadecimal representation of the character
\uXXXX Unicode16 hexadecimal representation of the character
\uXXXXXXXX Unicode32 hexadecimal representation of the character


Three:
raw string (raw string)
format:
r 'string content'
r "string content"
r '''string content'''
r """string content"""
Role: Let the escape symbol \ invalid
# escaped:

>> a = 'C:\newfile\test.py'
>> a
'C:\newfile\test.py'
#未转义raw字符串:
>> a = r'C:\newfile\test.py'
>> a
'C:\newfile\test.py'
>>


Four:
String operations:

  • The plus operator is used to concatenate strings
    . The += operator concatenates the original string with the string on the right to generate a new string
    Example:
    s = 'ABCD' + 'EFG'
    s += '123'
    print(s) > > ABCDEFG1234
    s2 = s + s
    print(s2) >> ABCDEFG1234ABCDEFG1234
    • Multiplication: generate duplicate strings
      = generate duplicate strings and bind the original variable to the resulting string.
      Example:
      s = "ABC"
      3 #s = 'ABCABCABC'
      print(s)
      s2 = 5 '0' #s2 = '00000'
      s = "123"
      s
      = 3 # s = '123123123'

Five:
String comparison operation:

            >
            >=
            <
                     <=
                        ==
                        !=

Format:
x > y
Comparison rules:
1, the first letter of the string x is compared with the first letter of the string y, if they are not the same, the comparison result is directly obtained. If it is the same, then go to the second letter for comparison, and so on,
2, the comparison is based on the UNICODE encoding value of the character
Example:

    'AD' > 'ABC'        #True
    'ABC' != 'CBA'    #True
    'ABC' == 'CBA'   #False
    'AB' <= 'ABC'      #True

Six:
in / not in operator
function: used in sequences, dictionaries, sets, and other containers to determine whether a value exists in the container, if it exists, it returns True, otherwise it returns False
not in in operator The return result is opposite,
format:
object in sequence


Seven:
the index of the string index
python string is an immutable sequence of characters.
Syntax:
string [integer expression]
Description: Python sequences can use index (index) to access objects in the
sequence The forward index of python sequences starts from 0, the second index is 1... And so on, the last index is len(s)-1
The reverse index of a python sequence starts at -1, -1 is the last, -2 is the second to last, and so on, the first is - len(s)
example:

>>s = "ABCD"
>>print(s[0]) #A
>>A
>>print(s[2]) #C
>>C
>>print(s[-1]) #D
>>D


Eight:
slice slice
function: take out the elements from the string and reconstitute a string sequence.
Syntax:
String [(start index b):(end index e)(:(step s))]
Note: The part enclosed in parentheses () means that it can be omitted.
Syntax description: 1, the start index is the position under the slice, 0 represents the first element, 1 represents the second element, -1 represents the next element from the left,
2, the end index is the end index of the slice (but not including the end point )
3, the step size is the offset
1 to the right after each slice obtains the current element, there is no step size, which is equivalent to moving one index position to the right after the value is obtained (default is 1)
2, when the step size is an integer , the most positive index is
3, when the step size is negative, the reverse slice
of the reverse slice is, the default start position is the last element, and the
end position is the previous position of the first element.
Example:

>> s = 'ABCDE'
>> s[0]
'A'
>> s[1]
'B'
>> s[2]
'C'
>> s[3]
'D'
>> s[4]
'E'
>> s[5] #没有第五个,所以报错
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: string index out of range

>> s = 'ABCDE'
>> s[-1]
'E'
>> s[-2]
'D'
>> s[-3]
'C'
>> s[-4]
'B'
>> s[-5]
'A'

>> s = 'ABCDE'
>> s[2:-2]
'C'
>> s[:-2]
'ABC'
>> s[:-2:2]
'AC'
>> s[-1:0:-1]
'EDCB'
>> s[-1:-100:-2]
'ECA'


Nine:
Sequence functions commonly used in python3:
len(seq) returns the length of the sequence
max(x) returns the maximum value of the sequence
min(x) returns the minimum value of the sequence

String encoding conversion function:
ord(c) returns the Unicode value of a character
chr(i) returns the character corresponding to the value i
Example:

 >>> i = ord('中')
 >>> i
20013
>>> c= chr(20013)
>>> c
'中'
>>> 

Ten:
Integer to string function:

hex(i)  将整数转换为  十六进制字符串
oct(i)  将整数转换为   八进制字符串
bin(i)  将整数转换为 二进制字符串
>>> hex(10)
'0xa'
>>> oct(10)
'0o12'
>>> bin(10)
'0b1010'
>>> 

String constructor:

str(obj='')     将对象转换为字符串

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324706616&siteId=291194637