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

How to use the splicing function of python

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces how to use the python splicing function, which is very detailed and has a certain reference value. Friends who are interested must finish reading it!

1. Split divides the large string into several substrings.

# a.split () # cut (by default split by space) You can also specify the parameter to cut according to XX (but the specified parameter will be cut off) msg = 'The day is a sunny day'result1 = msg.split () print (result1) result2 = msg.split (' y') # specify the parameter to cut off print (result2) result3 = msg.split (maxsplit=2) # represents a total of 2 cuts from the left print (result3) output: ['The',' day', 'is',' 'sunny',' day'] ['The da',' is a sunn', 'da','] ['The',' day','is a sunny day']

2. Partition looks from the left and returns the tuple according to the specified delimiter.

It contains: the separator on the left and the delimiter on the right.

# a.partition () # starts from the left and returns a tuple according to the specified delimiter. The tuple contains: the left side of the delimiter, the delimiter, the right side of the delimiter info = 'sz-18-180-12345678'result = info.partition (' -') result1 = info.partition ('|') # string, the left side is the original string On the right and in the middle are the empty string print (result) print (result1) output: ('sz',' -','18-180-12345678') ('sz-18-180-12345678,',')

3. Rpartition starts looking from the right.

Returns the tuple according to the specified delimiter, which contains the delimiter on the left and the delimiter on the right.

# a.rpartition () # starts on the right and returns a tuple according to the specified delimiter. The tuple contains: the content on the left side of the delimiter, the delimiter, the content on the right side of the delimiter info = 'sz-18-180-12345678'result = info.rpartition (' -') result1 = info.rpartition ('|') # string, the original string is on the right The left and middle are empty strings print (result) print (result1) output: ('sz-18-180,' -', '12345678') (',', 'sz-18-180-12345678')

4. The join connector splices the elements in the list into strings.

# a.join () # a.join () # use connectors to concatenate the elements in the list into a string names = ['Bob','Tom','Sam'] # loop each element And add the connector names_new1 ='- '.join (names) names_new2 =' .join (names) names_new3 =', '.join (names) print (names_new1) print (names_new2) print (names_new3) between elements: Bob-Tom-SamBob Tom SamBob,Tom,Sam above is all the content of the article "how to use python splicing function". Thank you for reading! Hope to share the content to help you, more related knowledge, 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

Development

Wechat

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

12
Report