1. String Value
The value of the string read by an index, starting with zero.
Take the following value range: string variable Name [starting index: End Index]. It contains the start, but does not include an end.
E.g:
str_my = "! hello, python I come!" Print (str_my [0:. 4]) # bits of 0, 1 print (str_my [0: 5] ) # of 0, 1, 4 # 6 from the beginning, has been taken to the end. Print (str_my [5:]) # scratch, taking the index subscript 7 Print (str_my [: 8]) Print (str_my [0: 8]) results: Hell the Hello !!, Python I came hello, py hello, py
2. String length
Get string length len function is generally used
str_my = "! hello, python I'm coming!" Print (len (str_my)) Results: 23
3. Find a substring
Syntax: string variable name .find (substring)
If found, returns starting index. If not found, returns -1.
E.g:
str_my = "hello, python! I'm coming!" # find Python Print (str_my.find ( "Python")) # Find ph Print (str_my.find ( "ph")) # Find! print ( "!" str_my.find () ) Results: . 6 -1 12 is
4. The replacement operation
Syntax: string variable .replace (old, new)
E.g:
str_my = "hello, python! I'm coming!" # be replaced with $! new_str = str_my.replace ( "!", "$") Print (new_str) # be replaced with $! and only replace one. = str_my.replace new_str (, "$", 1 "!") Print (new_str) result: the Hello, I came Python $ $ the Hello, Python $ here I come!
The string case conversion
Lowercase letters to uppercase letters: upper ()
Converting uppercase to lowercase: lower ()
The first letter is converted to uppercase, the rest lowercase: capitalize ()
The first letter of each word converted to uppercase, the rest lowercase: title ()
E.g:
# Lowercase letters to uppercase letters: Upper () str_my = "www.baidu.com" Print (str_my.upper ()) # uppercase letters are converted to lowercase: Lower () str_my = "www.baidu.com" Print ( str_my.lower ()) # the first letter is converted to uppercase, the rest lowercase: capitalize () str_my = "www.baidu.com" Print (str_my.capitalize ()) # the first letter of each word It is converted to uppercase, lowercase rest: title () str_my = "www.baidu.com" Print (str_my.title ()) results: www.baidu.com www.baidu.com www.baidu.com www.baidu.com
6. Remove string
Delete the string around (head and tail) on either side of the space or the specified string.
Syntax: string variable name .strip ([specified string])
E.g:
Example One:
str_a = "11python31, Class3" # delete the beginning and end of the space new_str = str_a.strip () Print (new_str) # delete the beginning and end of the specified 11 new_str2 = new_str.strip ( "1") Print (new_str2) # delete left empty character new_str3 = str_a.lstrip () Print (new_str3) # Remove the right null characters new_str3 = str_a.rstrip () Print (new_str3) result: 11python31, Class3 Python31, Class3 11python31, Class3 11python31, Class3
Example Two: str_a = '----- ++++ abc123' # remove both sides - and + null character print - (str_a.strip () strip ( '+').) Results: abc123
Example Three: str_a = 'ABC: 123' # delete a single fixed position of the character splicing microtome + # string splicing colon removed new_str_a str_a = [:. 3] str_a + [. 4:] Print (new_str_a) Results: abc123
7. string truncation
Truncated string is truncated according to a specified character string delimiter.
Syntax: string variable name .split (separator)
E.g:
str_b = "Hello everyone, I am a python. Today's weather is nice, very happy school. while on break." # Use .split (separator) split the string. = str_b.split the Result ( ".") Print (the Result) # specified number of times separated result_2 = str_b.split (, 1 ".") Print (result_2) Results: [ 'Hello everyone, I am a python', 'today Beautiful day, school is very happy, '' while on break ',' '] [' Hello everyone, I am a python ',' nice weather today, very happy school. While on break. ']
8. string concatenation
With the connector, the string which makes up a string list.
Requirements: a list which each value should be a string.
Syntax: Connector .join (list)
E.g:
list_a = [ 'Hello everyone, I'm Python', 'nice weather today, very happy school,' 'while on break', ''] list_b = "$ I connector $" .join (list_a) print (list_b) result: Hello everyone, I am a Python $ I $ connector nice weather today, very happy in class I connector $ $ $ intermission while I was resting on the connector $
9. The format string
Method 1: Use%
% S string
% D Digital
% F float
E.g:
str_a = "My goal this year is: rising wages% d, d reach a monthly salary%."% (5000, 10000) Print (str_a) Result: My goal this year is: wages rose 5000, to reach a monthly salary of 10000.
Second way: format
Placeholder{}
E.g:
str_a = "My goal this year is: {} wage inflation, reaching a monthly salary {}" .format (5000, 10000) Print (str_a) # 0} placeholder {1} { str_b = "My goal this year is: Salary {1} to rise, reaching a monthly salary {0} ".format (10000,5000) Print (str_b) # placeholders {0} {0} str_c =" {0} I am old, I hope I have been every year {0 }-year-old "format (18).. Print (str_c) result: my goal this year is: wages rose 5000, to reach a monthly salary of 10000 my goal this year is: wages rose 5000, to reach a monthly salary of 10,000 I am 18 years old, I hope I year He is 18 years old.
1. String Value
The value of the string read by an index, starting with zero.
Take the following value range: string variable Name [starting index: End Index]. It contains the start, but does not include an end.
E.g:
str_my = "! hello, python I come!" Print (str_my [0:. 4]) # bits of 0, 1 print (str_my [0: 5] ) # of 0, 1, 4 # 6 from the beginning, has been taken to the end. Print (str_my [5:]) # scratch, taking the index subscript 7 Print (str_my [: 8]) Print (str_my [0: 8]) results: Hell the Hello !!, Python I came hello, py hello, py
2. String length
Get string length len function is generally used
str_my = "! hello, python I'm coming!" Print (len (str_my)) Results: 23
3. Find a substring
Syntax: string variable name .find (substring)
If found, returns starting index. If not found, returns -1.
E.g:
str_my = "hello, python! I'm coming!" # find Python Print (str_my.find ( "Python")) # Find ph Print (str_my.find ( "ph")) # Find! print ( "!" str_my.find () ) Results: . 6 -1 12 is
4. The replacement operation
Syntax: string variable .replace (old, new)
E.g:
str_my = "hello, python! I'm coming!" # be replaced with $! new_str = str_my.replace ( "!", "$") Print (new_str) # be replaced with $! and only replace one. = str_my.replace new_str (, "$", 1 "!") Print (new_str) result: the Hello, I came Python $ $ the Hello, Python $ here I come!
The string case conversion
Lowercase letters to uppercase letters: upper ()
Converting uppercase to lowercase: lower ()
The first letter is converted to uppercase, the rest lowercase: capitalize ()
The first letter of each word converted to uppercase, the rest lowercase: title ()
E.g:
# Lowercase letters to uppercase letters: Upper () str_my = "www.baidu.com" Print (str_my.upper ()) # uppercase letters are converted to lowercase: Lower () str_my = "www.baidu.com" Print ( str_my.lower ()) # the first letter is converted to uppercase, the rest lowercase: capitalize () str_my = "www.baidu.com" Print (str_my.capitalize ()) # the first letter of each word It is converted to uppercase, lowercase rest: title () str_my = "www.baidu.com" Print (str_my.title ()) results: www.baidu.com www.baidu.com www.baidu.com www.baidu.com
6. Remove string
Delete the string around (head and tail) on either side of the space or the specified string.
Syntax: string variable name .strip ([specified string])
E.g:
Example One:
str_a = "11python31, Class3" # delete the beginning and end of the space new_str = str_a.strip () Print (new_str) # delete the beginning and end of the specified 11 new_str2 = new_str.strip ( "1") Print (new_str2) # delete left empty character new_str3 = str_a.lstrip () Print (new_str3) # Remove the right null characters new_str3 = str_a.rstrip () Print (new_str3) result: 11python31, Class3 Python31, Class3 11python31, Class3 11python31, Class3
Example Two: str_a = '----- ++++ abc123' # remove both sides - and + null character print - (str_a.strip () strip ( '+').) Results: abc123
Example Three: str_a = 'ABC: 123' # delete a single fixed position of the character splicing microtome + # string splicing colon removed new_str_a str_a = [:. 3] str_a + [. 4:] Print (new_str_a) Results: abc123
7. string truncation
Truncated string is truncated according to a specified character string delimiter.
Syntax: string variable name .split (separator)
E.g:
str_b = "Hello everyone, I am a python. Today's weather is nice, very happy school. while on break." # Use .split (separator) split the string. = str_b.split the Result ( ".") Print (the Result) # specified number of times separated result_2 = str_b.split (, 1 ".") Print (result_2) Results: [ 'Hello everyone, I am a python', 'today Beautiful day, school is very happy, '' while on break ',' '] [' Hello everyone, I am a python ',' nice weather today, very happy school. While on break. ']
8. string concatenation
With the connector, the string which makes up a string list.
Requirements: a list which each value should be a string.
Syntax: Connector .join (list)
E.g:
list_a = ['大家好,我是Python', '今天的天气真好,上课很开心', '一会儿就中场休息', ''] list_b = " $我是连接符$ ".join(list_a) print(list_b) 结果: 大家好,我是Python $我是连接符$ 今天的天气真好,上课很开心 $我是连接符$ 一会儿就中场休息 $我是连接符$
9.格式化字符串
方式一:%使用
%s 字符串
%d 数字
%f 浮点数
例如:
str_a = "我今年的目标是:薪资上涨%d,达到月薪%d。" % (5000,10000) print(str_a) 结果: 我今年的目标是:薪资上涨5000,达到月薪10000。
方式二:format
占位符{}
例如:
str_a = "My goal this year is: {} wage inflation, reaching a monthly salary {}" .format (5000, 10000) Print (str_a) # 0} placeholder {1} { str_b = "My goal this year is: Salary {1} to rise, reaching a monthly salary {0} ".format (10000,5000) Print (str_b) # placeholders {0} {0} str_c =" {0} I am old, I hope I have been every year {0 }-year-old "format (18).. Print (str_c) result: my goal this year is: wages rose 5000, to reach a monthly salary of 10000 my goal this year is: wages rose 5000, to reach a monthly salary of 10,000 I am 18 years old, I hope I year He is 18 years old.