1. Python strings usually have single quotes ('... '), Double quotation marks ("...") , Three quotation marks ("""..." "") Or ("'..." '). The string contained in the three quotes can be composed of multiple rows. Generally, it can represent the narrative string of a large segment. There is basically no difference in use, but double quotation marks and three quotation marks ("""..." "") Can contain single quotes, three quotes ("'..." ') Can contain double quotation marks without escaping.
2. Escape special characters with (/), such as (/), ('), and (/),('),(").
3. common string functions
(1) Str. Count () // returns the number of times a substring appears in the string.
(2) Str. Find () // returns a substring that appears at the starting position of the substring.
(3) Str. Lower () // converts all strings to lowercase letters.
(4) Str. Upper () // convert to uppercase
(5) Str. Split () // string to be separated. The string list is returned. It is separated by spaces by default.
(6) Len (STR) // returns the string length
For example:
>>> STR = 'hello, world'
>>> Str. Count ('O ')
>>> 2
>>> Str. Find ('lo ')
>>> 3
>>> Str. Lower ()
>>> 'Hello, world'
>>> Str. Upper ()
>>> 'Hello, world'
>>> Str. Split ()
>>> ['Hello, ', 'World']
>>> Str. Split (',')
>>> ['Hello', 'World']
>>> Len (STR)
>>> 13
>>> Str
>>> 'Hello, world'
All the above operations will not change the string itself!
4. String and number Conversion
Import string
String. atoi (STR [, base]) // base is an optional parameter, which indicates the hexadecimal type to convert characters
It is easy to convert a number into a string. STR () is used directly ()
5. Character and ASCII Conversion
Char-> ASCII ord ()
ASCII-> char CHR ()
Python string operations
1. Copy the string
# Strcpy (sstr1, SSTR2)
Sstr1 = 'strcpy'
SSTR2 = sstr1
Sstr1 = 'strcp2 ′
Print SSTR2
2. connection string
# Strcat (sstr1, SSTR2)
Sstr1 = 'strcat'
SSTR2 = 'append'
Sstr1 + = SSTR2
Print sstr1
3. Search for characters
# Strchr (sstr1, SSTR2)
Sstr1 = 'strchr'
SSTR2 = 'R'
NPOs = sstr1.index (SSTR2)
Print NPOs
4. compare strings
# Strcmp (sstr1, SSTR2)
Sstr1 = 'strchr'
SSTR2 = 'strch'
Print CMP (sstr1, SSTR2)
5. Check whether the scan string contains the specified characters.
# Strspns (sstr1, SSTR2)
Sstr1 = '000000 ′
SSTR2 = '000000 ′
# Sstr1 and chars both in sstr1 and SSTR2
Print Len (sstr1 and SSTR2)
6. String Length
# Strlen (sstr1)
Sstr1 = 'strlen'
Print Len (sstr1)
7. Convert lowercase characters in the string to uppercase characters
# Strlwr (sstr1)
Sstr1 = 'jcstrlwr'
Sstr1 = sstr1.upper ()
Print sstr1
8. append a string of the specified length
# Strncat (sstr1, SSTR2, n)
Sstr1 = '000000 ′
SSTR2 = 'abcdef'
N = 3
Sstr1 + = SSTR2 [0: N]
Print sstr1
9. String Length comparison
# Strncmp (sstr1, SSTR2, n)
Sstr1 = '000000 ′
SSTR2 = '123bc'
N = 3
Print CMP (sstr1 [0: N], SSTR2 [0: N])
10. Copy characters of the specified length
# Strncpy (sstr1, SSTR2, n)
Sstr1 ="
SSTR2 = '000000 ′
N = 3
Sstr1 = SSTR2 [0: N]
Print sstr1
11. String comparison, case insensitive
# Stricmp (sstr1, SSTR2)
Sstr1 = 'abcefg'
SSTR2 = 'abcefg'
Print CMP (sstr1.upper (), sstr2.upper ())
12. Replace the first n characters of the string with the specified characters.
# Strnset (sstr1, CH, n)
Sstr1 = '000000 ′
Ch = 'R'
N = 3
Sstr1 = N * CH + sstr1 [3:]
Print sstr1
13. Scan strings
# Strpbrk (sstr1, SSTR2)
Sstr1 = 'cekjgdklab'
SSTR2 = 'gka'
NPOs =-1
For C in sstr1:
If C in SSTR2:
NPOs = sstr1.index (c)
Break
Print NPOs
14. Flip string
# Strrev (sstr1)
Sstr1 = 'abcdefg'
Sstr1 = sstr1 [:-1]
Print sstr1
15. Search for strings
# Strstr (sstr1, SSTR2)
Sstr1 = 'abcdefg'
SSTR2 = 'cde'
Print sstr1.find (SSTR2)
16. Split the string
# Strtok (sstr1, SSTR2)
Sstr1 = 'AB, CDE, fgh, ijk'
SSTR2 = ','
Sstr1 = sstr1 [sstr1.find (SSTR2) + 1:]
Print sstr1
Reprinted from:Zookeeper cloudHttp://www.leexiang.info