Python strings, string operations, comparisons, indexes, slices, etc.

Source: Internet
Author: User

One:
String: str
Function: Used to record text information,
Literal representation method: the part enclosed in quotation marks is a string.
' Single quotation mark
"" Double quotation marks
"' Three quotes
"" "Three double quotes
The difference between single and double quotation marks:
Double quotation marks for single quotation marks do not count as Terminator
Single quotation marks in double quotation marks do not count as Terminator
The function of a three-quote string:
quotation marks and double quotation marks can be enclosed in three quotes
The newline character of the triple quote string is automatically converted to line feed ' \ n '

Second: Use escape sequences to represent special characters
Substring backslash in string literals \
followed by some characters that represent a special character
such as: \ ' represents a single quotation mark
\ "represents double quotation marks
\ n represents a newline character.
Backslash literal in string:
\ ' Single quotation mark
\ "Double quotation marks
\ back Slash
\ nthe line break
\ r returns cursor to beginning of line
\f Page Change
\ t Horizontal tab
\v Vertical Tab
\b Backspace
A string with zero character value
\xxx XX Two-bit hexadecimal representation of characters
\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" ""
Function: let escape symbol \ Invalid
#转义后的:

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

Four:
The operation of the string:

    • The plus operator is used to stitch strings
      The + = operator creates a new string with a concatenation of the original string and the right string
      Example:
      s = ' ABCD ' + ' EFG '
      s + = ' 123 '
      Print (s) >> ABCDEFG1234
      S2 = s + S
      Print (s2) >> abcdefg1234abcdefg1234
      • Multiply operations: Generate duplicate strings
        = generates a repeating string and binds the original variable to the string after it.
        Example:
        s = "ABC"
        3 #s = ' abcabcabc '
        Print (s)
        S2 = 5 ' 0 ' #s2 = ' 00000 '
        s = "123"
        s
        = 3 # s = ' 123123123 '

Five:
Comparison operation of strings:

            >            >=            <                     <=                        ==                        !=

Format:
X > Y
Comparison rules:
1, the first letter of the string x is compared to the first letter of the string y, and if it is not the same, it is directly to the comparison result. If the same, then go to the second letter to compare, 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 a sequence, dictionary, set, etc. to determine whether a value exists in a container, returns true if it exists, or false
The not-in-in operator returns the opposite result,
Format:
Object in sequence

Seven:
Index of the string of index
A python string is a sequence of characters that cannot be changed.
Grammar:
string [integer-expression]
Description: A python sequence can use an index to access an object in a sequence
The forward index of the Python sequence starts at 0, and the second index is 1 ... And so on, the last index is Len (s)-1
The reverse index of the Python sequence is starting from 1, 1 for the last, 2 for the penultimate, 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:
Sliced slice
Function: Remove the element from the string and re-form a sequence of strings.
Grammar:
string [(start index B):(End index E) (:(step s))]
Note: Some of the parentheses () are enclosed in a representation that can be omitted.
Syntax Description: 1, the start index is the position under the slice, 0 for the first element, 1 for the second element, 1 for the left and the next element,
2, the end index is the end index of the slice (but does not include the end point)
3, the step is the right offset of the slice each time the current element is fetched
1, no step, equivalent to the right to move the position of an index when the value is complete (default is 1)
2, when the step is an integer, the most positive index
3, when the step is negative, to reverse the slice
The reverse slice is the default starting position for the last element,
The terminating 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] #没有第五个, so error
Traceback (most recent):
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:
The sequence functions commonly used in Python3:
Len (SEQ) returns the length of the sequence
Max (x) returns the maximum value of a 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 that corresponds to the value of I
Example:

Ten:
convert integers to String functions:

hex(i)  将整数转换为  十六进制字符串oct(i)  将整数转换为   八进制字符串bin(i)  

String constructors:

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

Python strings, string operations, comparisons, indexes, slices, etc.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.