I String related operations
1. string splicing+
strvar = "Today is" + "Monday" strvar += ",Very happy today" print(strvar)
2. string repetition*
strvar = "Say important things three times" * 3 print(strvar)
3. string cross line splicing\
strvar = "sdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdf" \ "The extra lines are displayed on the second line" print(strvar)
4. string index
# Forward index 0123 strvar = "1234" # Reverse index-4-3-2-1
5. slicing of string: (slicing < = > interception)
# (1) [start index:] intercept from the start index to the end of the string strvar = "The night gave me black eyes,But I rolled my eyes" res = strvar[11:] print(res) # (2) [: end index] intercept from the beginning to before the end index (end index -1) res = strvar[:10] print(res) # (3) [start index: end index] intercept from the start index to before the end index (end index -1) res = strvar[8:10] print(res) # (4) [start index: end index: interval value] intercept characters at the specified interval from the start index to the end index # positive sequence res = strvar[::3] # 03691215 Intercept from start to end print(res) # Reverse order res = strvar[::-1] # -1 -2 -3 -4 -5 -6 -7 ... print(res) # (5) [:] or [::] intercepts all strings res = strvar[:] res = strvar[::] print(res)
II String correlation function
1.*capitalize string initial capital
strvar = "how old are you" res = strvar.capitalize() print(res)
2.title capitalize each word
strvar = "how old are you" res = strvar.title() print(res)
3.*upper capitalize all letters
strvar = "to be or not to be that is a question" res = strvar.upper() print(res)
4.*lower make all letters lowercase
res = strvar.lower() print(res)
5.*swapcase case case is interchangeable
strvar = "I Love You" res = strvar.swapcase() print(res)
6.*len calculate the length of the string
strvar = "adfs234sdfsa" res = len(strvar) print(res)
7.*count counts the number of an element in a string
strvar = "adfs234sdfsa" res = strvar.count("a") print(res)
8.*find find the index position of the first occurrence of a string (recommended)
"""character string.find("character",Start indexing,End index) If not found, return directly-1""" strvar = "oh Father this is my Favorate dog" res = strvar.find("F") res = strvar.find("F",4) res = strvar.find("Fav",5,10) # The end index itself cannot be retrieved. The previous value is retrieved print(res) # *The function of index is the same as that of find. If find fails, it will return -1. If index fails to find data, it will directly report an error # res = strvar.index("Fav",5,10) error
9.*startswitch judge whether it starts with a character or string
"""character string.startswith("character",Start indexing,End index) Return if present True,Otherwise return False""" strvar = "oh Father this is my Favorate dog" res = strvar.startswith("oh") res = strvar.startswith("this",10) res = strvar.startswith("this",10,13) # 10 11 12 print(res)
10.*endswitch judge whether it ends with a character or string
res = strvar.endswith("dog") res = strvar.endswith("rate",-12) res = strvar.endswith("rate",-12,-4) print(res)
11.*isupper determines whether all strings are capital letters
strvar = "ABCD" res = strvar.isupper() print(res)
12.*islower judge whether all strings are lowercase letters
strvar = "abcdd12345" res = strvar.islower() print(res)
13.*isdecimal check whether the string is composed of numbers. It must be pure numbers
strvar = "12354" strvar = "12354.8979112" res = strvar.isdecimal() print(res)
strvar = "abc"
14.ljust fill in the string, and the original character is left (fill in spaces by default)
res = strvar.ljust(10) print(res)
15.rjust fill in the string, with the original character to the right (fill in spaces by default)
res = strvar.rjust(10,"&") print(res)
16.*center fills in the string, and the original character is centered (blank is filled by default)
res = strvar.center(10) # Original string length + filled character length = 10, filled with spaces by default res = strvar.center(10,"#") print(res)
17.*strip removes the blank characters on both sides by default
strvar = "@@@@@ Jay Chou @@@@@" res = strvar.strip() res = strvar.strip("@") # Specify removed symbols print(res) strvar = "@@@@@ Jay Chou @@@@@" #rstrip remove a character on the right print( strvar.rstrip("@") ) #lstrip remove a character on the left print( strvar.lstrip("@") )
==Important==
18.*split splits a string into a list by a character (the default character is a space)
strvar = "you can you up no can no bb" lst = strvar.split() strvar = "you-can-you-up-no-can-no-bb" lst = strvar.split("-") # Separate from left to right lst = strvar.rsplit("-",2)# Separate from right to left, (you can specify the number of times to separate) print(lst)
19.*join concatenates a list into a string by a certain character (container types are acceptable)
lst = ['you', 'can', 'you', 'up', 'no', 'can', 'no', 'bb'] res = "-".join(lst) print(res)
20.*replace replace to replace the old character of the string with the new character
"""replace(Characters to replace,Replace with what,Number of replacements)""" strvar = "The cute little frog likes to eat mosquitoes,Yes no,Yes no,Any more" res = strvar.replace("Yes no","Really not") res = strvar.replace("Yes no","Really not",1) print(res)
III format of string
1. transfer parameters in sequence
strvar = "{}towards{}Fired a shot,Die with a silver bullet".format("Lizhihui","Minghao") print(strvar)
2. index parameter transfer
strvar = "During the exam{1},When playing{0}".format("Be submissive","Punch Quest ") print(strvar)
3. keyword parameter transfer
strvar = "{who2}Blew a kiss,{who1}Captivated".format(who1="Liucaixia",who2="Ma Shengping") print(strvar)
4. container type data (list or Yuanzu) transfer parameters
strvar = "{1[2]}towards{0[0]}Gave a wink,The nose bleeds more than 30000 feet,Bleed to death".format(["Sunxiangqun","Caochenguang","Songyunjie"],("Leah ","Sunzhihe","Wenziyue")) print(strvar) # In format, reverse subscript cannot be used and is not recognized strvar = "{group2[0]}towards{group1[-1]}Gave a wink,The nose bleeds more than 30000 feet,Bleed to death".format(group1 = ["Sunxiangqun","Caochenguang","Songyunjie"],group2 = ("Leah ","Sunzhihe","Wenziyue")) print(strvar) # If the container is a dictionary, write the key value directly without quotation marks strvar = "{group1[ccg]}towards{group2[1]}Gave a wink,The nose bleeds more than 30000 feet,Bleed to death".format(group1 = {"kxq":"Sunxiangqun","ccg":"Caochenguang","syj":"Songyunjie"},group2 = ("Leah ","Sunzhihe","Wenziyue")) print(strvar)
5. Use of padding symbols for format (^ > <)
^ Original string centered > Original string right < Original string left {who:*^10} who : Keyword parameter * : Characters to fill ^ : Original string centered 10 : Total length = Original string length + Fill character length strvar = "{who:*^10}stay{where:>>10},{do:!<10}".format(who="Liu Peng",where="cinema",do="learning") print(strvar)
6. use of special symbols such as hexadecimal conversion (: d:f:s:,)
# : d integer placeholder (type must be integer) strvar = "Liuzihao bought it yesterday{:d}A toilet water".format(100) # 100.5error print(strvar) # : 2d takes up two digits. If it is not enough, fill in the blank space. It is on the right by default strvar = "Liuzihao bought it yesterday{:2d}A toilet water".format(3) # < > ^ adjust the corresponding position strvar = "Liuzihao bought it yesterday{:<2d}A toilet water".format(3) strvar = "Liuzihao bought it yesterday{:^3d}A toilet water".format(3) print(strvar) # : f floating point placeholder (type must be floating point) strvar = "When Liu Xin graduated,The salary for job hunting is{:f}".format(2.5) # : 2f keep 2 decimal places strvar = "When Liu Xin graduated,The salary for job hunting is{:.2f}".format(2.56789) print(strvar) # : s string placeholder (required type must be string) strvar = "{:s}".format("It's a nice day today,Cloudless") print(strvar) # :, money placeholder strvar = "{:,}".format(123456789) print(strvar) # Comprehensive case strvar = "The average annual salary of the students after graduation is{:.1f},You can buy it in Beijing{:d}suite,I feel very{:s}".format(600000.681,1,"Great") print(strvar)
IV List related operations
1. list splicing (same tuple)
lst1 = [1,2,3] lst2 = [4,5,6,6] res = lst1 + lst2 print(res)
2. list repetition (same tuple)
res = lst1 * 3 print(res)
3. slicing of list (same tuple)
Syntax = > list [::] full format: [start index: end index: interval value]
#(1) [start index:] intercept from the start index to the end of the list
#(2) [: end index] intercept from the beginning to before the end index (end index -1)
#(3) [start index: end index] intercept from the start index to before the end index (end index -1)
#(4) [start index: end index: interval value] intercept the list element value at the specified interval from the start index to the end index
#(5) [:] or [::] intercept all lists
lst = ["LV Dongbin","Hexiangu","Iron abduction plum","Cao Guojiu","Zhangguolao","Lancaihe","Hanxiangzi","Wang Wen"]
(1) [start index:] intercept from the start index to the last of the list
res = lst[2:] print(res)
(2) [: end index] intercept from the beginning to before the end index (end index -1)
res = lst[:4] print(res)
(3) [start index: end index] intercept from the start index to before the end index (end index -1)
res = lst[4:6] print(res)
(4) [start index: end index: interval value] intercept the list element values at the specified interval from the start index to the end index
# Forward intercept res = lst[::2] # 0 2 4 6 8 ... print(res) # Reverse interception res = lst[::-2] # -1 -3 -5 -7 -9 print(res)
(5) [:] or [::] intercept all lists
res = lst[:] res = lst[::] print(res)
4. list acquisition (same tuple)
# 0 1 2 3 4 5 6 7 lst = ["LV Dongbin","Hexiangu","Iron abduction plum","Cao Guojiu","Zhangguolao","Lancaihe","Hanxiangzi","Wang Wen"] # -8 -7 -6 -5 -4 -3 -2 -1 res = lst[7] res = lst[-1] print(res)
5. list modification (slicable)
""" The required data type is Iteratability data(Container type data,range object,iterator ) """ lst = ["LV Dongbin","Hexiangu","Iron abduction plum","Cao Guojiu","Zhangguolao","Lancaihe","Hanxiangzi","Wang Wen"] # Multiple elements can be modified at a time by using slices, without any limit on the number # lst[1:3] = "abcd" lst[3:5] = ["Round trip","Yanguozhang","Herdsman"] print(lst) # Slice matching step, how many elements are cut out and how many elements are modified lst = ["LV Dongbin","Hexiangu","Iron abduction plum","Cao Guojiu","Zhangguolao","Lancaihe","Hanxiangzi","Wang Wen"] # res = lst[::2]#LV Dongbin, tie guai, Li Zhangguo, Lao hanxiangzi # lst[::2] = "abcd" lst[::2] = range(1,5) # 0 2 4 6 8 10 ..... print(lst,"<==>")
6. deletion of list (slicable)
lst = ["LV Dongbin","Hexiangu","Iron abduction plum","Cao Guojiu","Zhangguolao","Lancaihe","Hanxiangzi","Wang Wen"] # del lst[-1] # print(lst) # What is deleted is the variable res itself, not the element in the list """ res = lst[-1] del res print(lst) """ # del lst[:2] del lst[::3] # 0 3 6 9 12 ... print(lst) # A list of tuples in which elements can be modified; tup = (1,2,3,[4,5,6,(7,8,9)]) tup[-1][1] = 6666 print(tup)