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

What are the common string operations in Python3

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

In this issue, the editor will bring you about the common string manipulation in Python3. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

1. Str.strip removes the character specified in the first bit of the string, and the white space character intput= "nnn entry nnn" print ('[% s]'% input) # strip does not change the value of the original string by default. So you need a new variable to receive newInput=input.strip () print ('[% s]'% newInput) # remove the first n character print ('[% s]'% input.strip ('n')) # remove the first n character + white space character print ('[% s]'% input.strip ('n'). Strip ()) [getting started] [getting started] [getting started] ] 2. Str.center aligns the string with the specified character intput= "getting started" print (input.center (12) Str.count () counts the number of times the string appears in another string # Syntax str.count (value, start, end) # start starting index optional # end ending index optional input= "getting started with rumen" print (input.count ("getting started")) # specify search scope print (input.count ('getting started', 2Pol 7)) 414. Str.find () finds the location where the string first appears # Syntax str.find (value,start,end) # start starting index optional # end ending index optional # if not found, return-1input = "getting started rumen getting started" print (input.find ('getting started ")) # specified range print (input.find (getting started, 2 Magne7)) 035. Str.rfind (value,start,end) finds the last position of the string # Syntax str.rfind (value,start,end) # start starting index optional # end ending index optional # if not found, return-1input = "getting started rumen" print (input.rfind ('getting started ") # specified range print (input.rfind (' getting started', 2 Magne7)) 1736. Str.swapcase () returns a copy of the string and converts the case of the string to input= "ru men XIAO zhan" print (input.swapcase ()) RU MEN xiao ZHAN7. Str.startswith () determines whether a string starts with a string # syntax: str.startswith (prefix [, start [, end]])-> bool# start start index optional # end end index option # return Bool value input= "getting started" print (input.startswith ('getting started')) # specify range print (input.startswith ('getting started') 2) TrueFalse8.str.endswith () determines whether the string ends with a string # str.endswith (suffix [, start [, end]])-> bool# start start index optional # end end index option # returns Bool value input= "entry site" print (input.endswith ('small station')) # specified range print (input.endswith ('small station', 2)) TrueTrue9. Str.split () splits the string into a list. The default separator is any number of white space characters # syntax: str.split (self, /, sep=None, maxsplit=-1) input= "n getting started n" print (input.split ()) # specifies the split character print (input.split ('n')) ['n','n'] ['','n'] ['', 'getting started' 'small station','] 10. String case conversion # 10.1 str.capitalize () converts the first character of the string to uppercase input= "ru mEn z" print (input.capitalize ()) # 10.2 str.upper () converts all characters to uppercase print (input.upper ()) # 10.3 str.title () converts the first character of each word to uppercase, and the rest to lowercase print (input.title ()) Ru men z RU MEN Z Ru Men Z11. The first bit of the str.ljust () and str.rjust () strings is filled to the specified length with the specified characters. The default is to fill in # syntax with spaces: ljust (self, width, fillchar='', /) # syntax: rjust (self, width, fillchar='', /) # width is the length of the filled string input= "rumenz" print (intput.ljust (20 pencil)) print (input.rjust (20) entry # $rumenz12. Str.zfill () string is preceded by "syntax: str.zfill (self, width, /) # width string the length of the filled string input=" rumenz "print (intput.zfill (20)) 0000000000000000 entry site 13. Define a multiline string input=''' entry rumenz'''print (input) entry rumenz14. Determine whether the string is in another string ints= "rumenz" print ('one' in ints) False15. Is determines the same str1= "rumenz" str2= "rumenz" print (str1 is str2) True16 when judging the string address. Determine the type of string # str.isalnum () determine whether the string is composed of letters and numbers str1= "rumenz123" print (str1.isalnum ()) # str.isalpha () determine whether the string is composed of letters print (str1.isalpha ()) # str.isdigit () determine whether the string is pure numeric print (str1.isdigit ()) TrueFalseFalse17. The array is spliced into a string # syntax. Join (str) li= ['getting started', 'small station', '123'] print ('. Join (li)) print ('-'. Join (li)) getting started-station-12318. String substitution # Syntax str.replace (self, old, new, count=-1, /) # old the number of new strings # count to replace with the old string # new, default is-1, all replace str1= "getting started" print (str1.replace ("getting started")) rumen rumen19. String mapping # a-> 1b-> 2c-> 3d-> 4e-> 5f-> 6p=str.maketrans ('abcdef','123456') print (' aaacccd'.translate (p)) 1113334

These are the common string operations in the Python3 shared by the editor. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

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