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 do string processing and text Analysis with Python

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

Share

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

This article mainly introduces "how to carry out string processing and text analysis in Python". In daily operation, I believe many people have doubts about how to deal with string processing and text analysis in Python. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to handle strings and text analysis in Python". Next, please follow the editor to study!

Space stripping

Space stripping is the basic operation of dealing with strings. The common methods are lstrip () (stripping check-in spaces), rstrip () (stripping trailing spaces), strip () (stripping leading and trailing spaces).

S = 'This is a sentence with whitespace. \ n'

Print ('Strip leading whitespace: {}' .format (s.lstrip ()

Print ('Strip trailing whitespace: {}' .format (s.rstrip ()

Print ('Strip all whitespace: {}' .format (s.strip ()

Strip leading whitespace: This is a sentence with whitespace.

Strip trailing whitespace: This is a sentence with whitespace.

Strip all whitespace: This is a sentence with whitespace.

Of course, there are many of the same methods, and another common one is to deal with strings by specifying the characters you want to split:

S = 'This is a sentence with unwanted characters.AAAAAAAA'

Print ('Strip unwanted characters: {}' .format (s.rstrip ('A')

String splitting

String splitting uses split () in Python to split a string into a smaller list of strings.

S = 'KDnuggets is a fantastic resource'

Print (s.split ())

When no parameter is added, split () splits by default based on spaces, but it can also split strings by specified characters.

S = 'these,words,are,separated,by,comma'

Print ('\',\ 'separated split-> {}' .format (s.split (','))

S = 'abacbdebfgbhhgbabddba'

Print ('\'b\ 'separated split-> {}' .format (s.split ('b')

',' separated split-> ['these',' words', 'are',' separated', 'by',' comma']

'b' separated split-> ['asides,' ac', 'de',' fg', 'hhg',' asides, 'dd', 'a']

Synthesize list elements into strings

The above talked about how to split a string into many, here is how to combine many strings into a single string. Then the join () method is used.

S = ['KDnuggets',' is', 'asides,' fantastic', 'resource']

Print ('.join (s))

KDnuggets is a fantastic resource

String inversion

There is currently no method for string inversion in Python, but we can first treat a string as a list of multiple characters and reverse the entire string by reversing table elements.

Case conversion

The case conversion of strings in Python is still very simple. You only need to make good use of the three methods of upper (), lower () and swapcase () to achieve the conversion between case and case.

S = 'KDnuggets'

Print ('\ 'KDnuggets\' as uppercase: {} '.format (s.upper ()

Print ('\ 'KDnuggets\' as lowercase: {} '.format (s.lower ()

Print ('\ 'KDnuggets\' as swapped case: {} '.format (s.swapcase ()

'KDnuggets' as uppercase: KDNUGGETS

'KDnuggets' as lowercase: kdnuggets

'KDnuggets' as swapped case: kdNUGGETS

Check if there are any string members

The easiest way to detect string members in Python is to use the in operator. Its grammar is very similar to natural language.

S1 = 'perpendicular'

S2 = 'pen'

S3 = 'pep'

Print ('\ 'pen\' in\ 'perpendicular\'-> {} '.format (S2 in S1))

Print ('\ 'pep\' in\ 'perpendicular\'-> {} '.format (S3 in S1))

'pen' in 'perpendicular'-> True

'pep' in 'perpendicular'-> False

Of course, if you want to find a specific location, not just to detect the existence of characters, you need to use the find () method.

S = 'Does this string contain a substring?'

Print ('\ 'string\' location-> {} '.format (s.find (' string')

Print ('\ 'spring\' location-> {} '.format (s.find (' spring')

'string' location-> 10

'spring' location->-1

By default, find () returns the index of the first character of the substring that first appears, or-1 if the substring cannot be found.

Substring substitution

What if we want to replace the string after we find it? That requires the functionality of the replace () method.

S1 = 'The theory of data science is of the utmost importance.'

S2 = 'practice'

Print ('The new sentence: {}' .format (s1.replace ('theory', S2)

The new sentence: The practice of data science is of the utmost importance.

If the same substring occurs multiple times, you can use the count parameter option to specify the maximum number of consecutive substitutions.

At this point, the study on "how to do string processing and text analysis in Python" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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