Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Center find join s, a common method for 32 strings.

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

The method commonly used in lesson 8: center method # string method: the function of center# is to center the string in a certain width area to display # this method is the same as the ^ in format before # ^ print (") #

< hello >

#

< hello >

Print (".format (" hello ")) #

< hello >

# print (") # print (" .format ("hello")) #-the method commonly used in lesson 9: the find method looks for substrings in a large string if it is found The find method returns the position where the first character of the substring appears in the large string, which is the index if it is not found. Then the find method returns-1find method has three parameters, the first is the substring to find, the second parameter is the start index, the third parameter is the end index left closed right open # string method: finds = "hello world" print (s.find ("world")) # 6w the location index is 6 print (s.find ("abc")) #-1 does not return-1 print (s.find ("o")) # 4 returns the index print of the first 0 (s.find ("o") 6)) # 7 the second 0 appears in the seventh position print (s.find ("l", 5pr 9)) #-1print (s.find ("l") 5 (join# 10)) # 9 left close and right open-the method commonly used in lesson 10: string method: join method # string method: join# is used to connect elements in a sequence Inverse method of split method list = ["a", "b", "c", "d" "e"] s ='* 'print (s.join (list)) # a*b*c*d*eprint ("xy" .join (list)) # axybxycxydxye# C:\ abc\ xyz# / abc/xyz# c:\ usr\ local\ nginx\ # / usr/local/nginx/# this join is very helpful in our linux and windows how the paths of different operating systems are shown in python code: to represent a linux environment The path # defines a tuple dirs = ('' 'usr','local','nginx','') linuxPath =' / '.join (dirs) print (linuxPath) # / usr/local/nginx/windowPath = (' CJV'+'\\ '.join (dirs)) print (windowPath) # C:\ usr\ local\ nginx\ # numList = [1JI 2JI 3JI 4 5J6] # this escape exception must be converted into a string # print ("a" .join (numList))-the common method in lesson 11: the split method is split into a list # character String method: split the split method into a list S1 = "a b c d e f" print (s1.split ()) # ['a' Path = "/ usr/local/nginx" pathList = path.split ('/') print (pathList) # [', 'usr',' local' 'nginx'] windowPath = "D:" + "\\" .join (pathList) print (windowPath) # D:\ usr\ local\ nginx--- commonly used methods in lesson 12 strings: lower, Upper and capwords function lower: make all English letters lowercase upper: make all English letters uppercase capwords function: capitalize the first letter of a separate string string method: lower, upper and capwords function print ("HEllo" .lower ()) # helloprint ("hello" .upper ()) # HELLOlist = ["Python" "Ruby", "Java" "KOTLIN"] if "Kotlin" in list: print ("Kotlin found") else: print ("Kotlin not found") # No Kotlin case sensitive for lang in list: # loop through so that every word is converted to lowercase if "kotlin" = = lang.lower (): print ("found Kotlin") # found Kotlin break From string import capwordss = "i not only like Python, but also like Kotlin" print (capwords (s)) # I Not Only Like Python But Also Like Kotlin--- lesson 13 commonly used methods in strings: replace method and strip method replace method: replace the same substring that appears in a string The most common thing here is to replace all the spaces entered by the console with blanks. For example: "" IDE = input ('Please enter the name of IDE') findIDE = IDE.replace (','). Lower () # converts the input space to blank and the input value to lowercase "" strip method: intercepts the space before and after # string method: replace and strips = "abcdaedf" print (s.replace ("a", "12345")) # 12345bcd12345edfprint (s.replace ("xyz") "aa") # abcdaedf returns the original string print ("geekori.com" .strip ()) # geekori.comprint ("

< geekori.com >

".strip () #

< geekori.com >

Will only intercept the spaces before and after Will not intercept the middle space langList = ["python", "java", "ruby", "scala" "perl"] lang = "python" if lang in langList: print ("python found") else: print ("python not found") # not found pythonif lang.strip () in langList: print ("python found") else: print ("python not found") # found pythons = "* * $$* Hello * World * * $" print (s.strip ("* $")) # Hello * World "* $" these three are or relationships-methods commonly used in strings in lesson 14: translate methods and maketrans methods # string methods: translate and maketrans# translate: replace a single character Maketrans method: converted to dictionary s = "I not only like python" But also like kotlin. "table = s.maketrans (" ak "," * $") # what he means is to replace a with * k with $print (table) # {97: 42,107: 36} first forward to the dictionary ACLLZ (s.translate (table)) # I not only li$e python, but * lso li$e $otlin.table1 = s.maketrans (" ak "," * $") ") # the third parameter deletes the whole meaning: convert a to *, k to $and delete the spaces in the middle print (table1) # {97: 42,107: 36,32: None} print (s.translate (table1)) # Inotonlyli$epython,but*lsoli$e$otlin.# delete all spaces after

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report