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 is the method of dividing and splicing Python strings?

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

Share

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

This article mainly talks about "what is the method of dividing and splicing Python strings". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what is the method of dividing and splicing Python strings?"

There are a lot of things that have to be said between strings and list. For example, if some students want to use python to automatically grab the download link on a web page, they need to deal with the code of the web page. In the process of processing, it is inevitable to do a lot of operations between the string and the list.

Let's start with the basics. Suppose you have an English sentence now, and you need to take out each word in the sentence and deal with it separately.

Sentence ='I am an Englist sentence'

At this point, you need to split the string.

Sentence.split ()

Split () splits the string according to the spaces in it, each segment is a new string, and finally returns these strings to form a list. So get it.

['am',' an', 'Englist',' sentence']

The space in the original string no longer exists.

In addition to spaces, split () is also divided by the newline character\ n and the tab character\ t. So it should be said that split is divided by white space characters by default.

The default is because split can also specify split symbols. For example, you have a very long string.

Section ='Hi. I am the one. Bye.'

Each sentence can be separated by specifying the split symbol as'.'

Section.split (.')

Get

['Hi',' I am the one', 'Bye',']

At this time, O'Neill. It is removed as a delimiter, while the space remains in its position.

Notice the last empty string. Every'.' Will be used as a delimiter, and even if there are no other characters behind it, an empty string will be split. For example

'aaa'.split ('a')

You will get [','], a list made up of four empty strings.

Since you can split a string, whether there should be string concatenation or not, let's look at the second word, join.

Split is a list that splits a string into many strings, while join concatenates all the strings in a list into a single string.

The format of join is a little strange. It is not a method of list, but a method of a string. First you need to have a string as the connector for all elements in the list, and then call the join method of the connector. The argument to join is the concatenated list:

S =';'

Li = ['apple',' pear', 'orange']

Fruit = s.join (li)

Print fruit

Get the result 'apple;pear;orange'.

As you can see from the result, the semicolon concatenates several strings in list.

You can also enter it directly in shell:

';' .join (['apple',' pear', 'orange'])

Get the same result.

The string used to connect can be multiple characters or an empty string:

'.join (['hello',' world'])

The resulting 'helloworld', strings are seamlessly concatenated together.

At this point, I believe that everyone on the "Python string segmentation and splicing method is what" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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