python data type (string) V

String double quotation marks "" or apostrophe '' can be.

(A) string concatenation

= S1 " numbers: " 
A = 33 is 
B = STR (A) # use str () converts the value into a string
c = 55
Print (S1 + B)
 Print (A + C)
 Print (S1 + STR (A))
 Print (S1 + A) # string values directly spliced, program error

result:

Digital: 33
88 
Number: 33
Traceback (most recent call last):
  File "D:/untitled/demo.py", line 296, in <module>
    print(s1+a)
TypeError: can only concatenate str (not "int") to str

 

(B) the string sections

Code:

= s ' 0123456789987654321 ' 
Print (s [0: 5 ])
 # obtain the substring s index from 0 to 5 at index (no) of

Print (s [0: -5 ])
 # get a substring s from index 0 to the penultimate 5 (not included) of the string

Print (s [-8: -4 ])
 # get a substring s 8 from the penultimate to the penultimate string 4 (not included) of the string

Print (s [-18: 6 ])
 # get a substring s 18 from the penultimate to the string index 6 (not included) of the string

Print (S [:: 2 ])
 # every character takes a

Print (S [::. 3 ])
 # every two, takes a character

Print (s [. 3 :])
 # get a substring from index s to the end of the string of three

Print (s [-7 :])
 # get a substring s from the reciprocal of the seventh character to the end

Print (s [: 7 ])
 # get a substring s 7 (not included) from the index 0 to the index

Print (s [: - 7 ])
 # get a substring s index from 0 to 7, the reciprocal (not included) of

result:

01234
01234567899876
8765
12345
0246897531
0369741
3456789987654321
7654321
0123456
012345678998

(C) len () function

len () function to get the string length or number of bytes.

Code:

= b ' 123 I was little cute? ' 
Print (len (B))
 # calculates the length of the character 
Print (len (b.encode ()))
 # string after encoding acquired its bytes 
# characters plus Chinese punctuation of 7, 7 * 3 accounting for 21 bytes, and the letters and numbers, and punctuation, 3 bytes, occupies a total of 24 bytes. 
= A ' No you're not! ' 
Print (len (A))
 Print (len (a.encode ()))

result:

10
24
6
18

 

(D) count () function

Statistical functions frequency occurrence of the string count ().

Code:

= STR ' 01234567890123456789 ' 
# search string at the specified index 
Print (str.count ( ' 12345678 ' , 0, -2 ))
 # from index 0 to the inverse of a second (not included) queries the query string designated number (the number to find 012345678901234567 12345678)

Print (str.count ( ' 1234567 ' , 0, -2 ))
 # from the index 0 to the inverse of a second (not including) the query string specified number (012345678901234567 find the number 1234567)

Print (str.count ( ' 3456 ' , 3,7 ))
 # (3456 in the number of queries 3456) from the index to the index 7 3 (not included) query string specified number

Print (str.count ( ' 3456 ' , 3,6 ))
 # (3456 Discover number 345) from the index of the index 3 to 6 (not included) query string specified number

Print (str.count ( ' 3456 ' , 3,17 ))
 # (3456 in the number of queries 34567890123456) from the index to the index 17 3 (not included) query string specified number

Print (str.count ( ' 3456 ' , 3,16 ))
 # (3456 in the number 3456789012345 query) from the index to the index 16 3 (not included) query string specified number

Print (str.count ( ' 23456 ' , -18, -3 ))
 # from the first to the third countdown at 18 reciprocal (not included) query string specified number (the number of queries 234567890123456 23456)

Print (str.count ( ' 23456 ' , -18, -4 ))
 # from the first to the third countdown at 18 reciprocal (not included) query string specified number (the number of queries 23456789012345 23456)

result:

1
2
1
0
2
1
2
1

 

Five) split () function

split () function is used to split a string.

Code:

= str " Hello! Welcome to embark on this road of no return yards agriculture. bailing? >>> see trick! hahaha " 
List1 = str.split () # default separator is divided (by default be in the form of a space) 
Print (list1)
List2 = str.split ( ' >>> ' ) # using a plurality of characters divided 
Print (List2)
list3 = str.split ( ' . ' ) # adopted. No. split 
Print (list3)
list4 = str.split ( '  ' , 4) # use space is divided, and only up to a predetermined sequence is divided into four sub- 
Print (list4)
list5 = str.split ( ' > ' ) # employed> characters divided 
Print (list5)

result:

[ ' Hello! Welcome to embark on this road of no return yards agriculture. ' , ' Bailing? ' , ' >>> ' , ' see trick! hahaha ' ]
[ ' Hello! Welcome to embark on this road of no return yards agriculture. Bailing? ' , ' See trick! hahaha ' ]
[ ' Hello! Welcome to embark on this road of no return yards agriculture ' , ' bailing? >>> see trick! hahaha ' ]
[ ' Hello! Welcome to embark on this road of no return yards agriculture. ' , ' Bailing? ' , ' >>> ' , ' see trick! hahaha ' ]
[ ' Hello! Welcome to embark on this road of no return yards agriculture. Bailing? ' , '' , '' , ' See trick! hahaha ' ]

 

(Vi) format () function

format () method of the string format.

Code:

# Displayed in monetary 
Print ( " monetary: {D} :, " .format (1000000 ))
     # scientific notation 
Print ( " scientific notation: {:} E " .format (1200.12 ))
     # hexadecimal hexadecimal representation 
Print ( " hex 100: {: # X} " .format (100 ))
     # output percentage 
Print ( " percentage represents 0.01: 0 {%} :. " .format (0.01 ))

Print ( " monetary Output: {D} :, " .format (875 638 768 ))
 Print ( " in hexadecimal output: {: # X} " .format (100 ))
 Print ( " form output scientific notation: {:} E " .format (1567.786 ))
 Print ( " percentage represents 0.19: 0 {%} :. " .format (0.19))

result:

Monetary: 000,000 
scientific notation: 1.200120E + 03 
100 hex: 0x64 
percentage representation 0.01: 1% 
monetary output: 875,638,768 
hexadecimal output: 0x64 
forms output scientific notation: 1.567786E + 03 
0.19 percentage represents: 19%

 

Guess you like

Origin www.cnblogs.com/abcd8833774477/p/11779894.html