[Python Study Notes] string processing tips (continuous update)

Source: Internet
Author: User

"" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "", "" "". "" "" "" "" "" "" "" "," "" "" "," "" "" "," "" "," "" ">> String processing .py>> Author: Liu Yang>> e-mail: [email protected]>> blog: Www.cnblogs.com/liu66blog "" " "" "" "" "" "" "" "" "" "" "" "" "" "" "" "." "" "" "" "" "" "" "" "" "" "" "" "" #!/usr/bin/env python#-*-. " Coding:utf-8-*-import sys, os# 1. Connection and merging of strings # Add//Two strings can be conveniently connected by ' + ' str1= ' 123 ' str2= ' 456 ' Str3=str1+str2print (STR3) # -----------Output----------------------# 123456#------------------------------------# Merge//join method url=[' www ', ' cnblog ' , ' Com/liu66blog ']print ('. '). Join (URL)) #-----------Output----------------------# www.cnblog.com/liu66blog#------------------------------------# 2. String slice and multiply # multiply//For example write code to delimiter, with Python easy to implement separator= ' * ' *30print (Separator) #-----------Output----------------------# #------------------------------------# slicing Operation Url= ' Www.cnblogs.com/liu66blog ' # Remove the label 0-15 characters print (url[0:16]) # Remove the label 16-Last Print (url[16:]) # Take the bottom fourth to the last print (URL[-4:]) # Copy string print (url[::]) #-----------Output----------------------# www.cnblogs.com/# liu66blog# blog# www.cnblogs.com/ liu66blog#------------------------------------# 3. The segmentation of the string # Ordinary division, with split# split can only do very simple segmentation, and does not support multiple separate url= ' Www.cnblogs.com/liu66blog ' Url_list=url.split ('. ') Print (url_list) #-----------Output----------------------# [' www ', ' cnblogs ', ' Com/liu66blog ']#------------------------ ------------# Complex Partition # R means not escaped, delimiter can be, or, or/, or a space followed by 0 additional spaces, and then follow this pattern to split url= ' www.cnblogs.com/liu66blog ' import reurl _list=re.split (R ' [.;/] \s* ', url) print (url_list) #-----------Output----------------------# [' www ', ' cnblogs ', ' com ', ' liu66blog ']#------------- -----------------------# 4. Processing of the beginning and end of a string # For example, we're going to find out what the first name is or what the end Url= ' Www.cnblogs.com/liu66blog ' result= Url.endswith (' blog ') print (result) result=url.startswith (' ww. ') Print (Result) #-----------Output----------------------# true# false#------------------------------------# 5. String lookup and Match # General Find # We can easily find substrings in a long string, returning the index where the substring is located, and if it cannot find the return -1url= ' Www.cnblogs.com/liu66blog ' reSult=url.find (' liu66 ') print (result) result=url.find (' Liuyang ') print (result) #-----------Output---------------------- # 16# -1#------------------------------------# Complex Find data_str= ' 2018/2/22 ' Result=re.match (R ' \d+/\d+/\d+ ', data_str) if Result:print (' OK, Present ') #-----------output----------------------# OK, #------------------------------------# 6. Substitution of Strings # Normal replacement//with replace can be url= ' Www.cnblogs.com/liu66blog ' url_new=url.replace (' www. ', ') print (url_new) #----------- Output----------------------# cnblogs.com/liu66blog#------------------------------------# Complex replacement using re.sub function Url= ' Www.cnblogs.com/liu66blog ' Url_new=re.sub (R ' \d\d ', ' xx ', url) print (url_new) #-----------Output----------------------# cnblogs.com/liu00blog#------------------------------------# 7. Remove some characters from the string # Remove the space//For text processing such as reading a line from a file, Then you need to remove the spaces on either side of each line, table or newline character url= ' Www.cnblogs.com/liu66blog ' Url_new=url.strip () print (url_new) # Complex text cleanup, You can use str.translate,# to build a conversion table, table is a translation table, the ' W ' is converted to uppercase ' W ', # and then the ' liu66 ' is removed from the old_str, then the remaining strings are translated by table Python3.4 has no string.make.Trans (), instead of the built-in function: # Bytearray.maketrans (), Bytes.maketrans (), Str.maketrans () url= ' Www.cnblogs.com/liu66blog ' # Create translation Table instr= ' W ' outstr= ' W ' Table=str.maketrans (INSTR,OUTSTR) url_new=url.translate (table) print (url_new) #---------- -Output----------------------# WWW.cnblogs.com/liu66blog#------------------------------------# 8. Find the longest word txt= ' Python is A programming language that lets-more quickly and integrate your systems more effectively. "You can learn to the use of Python and see almost immediate gains in productivity and lower maintenance costs. ' Learn more about Python ... ' # Use spaces to separate Txt_list=txt.split (") # Use the sorted () function to sort by word length txt_list_new=sorted (txt_list,key=lambda x:len (x), reverse=true # defines an empty list, storing the longest longest_word=[]# to determine the length of the word behind for I,word in Enumerate (txt_list_new): If Len (Txt_list_new[i]) <len (txt_ List_new[0]): Break Else:longest_word.append (Txt_list_new[i]) print (longest_word) #-----------Output--------- -------------# [' Effectively. ', ' productivity ']#------------------------------------# 9. Find words of the specified length Len_4_word=filter (lambda x:5>len (x) >=4,txt_list) # Note Python3 filter returns no longer a list Need to convert yourself!! Len_4_word_list=list (Len_4_word) # Convert to go-heavy progenitor len_4_word_tuple=tuple (set (len_4_word_list)) print (len_4_word_list) Print (len_4_word_tuple) #-----------Output----------------------# [' That ', ' lets ', ' work ', ' more ', ' your ', ' more ', ' more '] # (' Your ', ' more ', ' lets ', ' so ', ' work ') #------------------------------------# 10. The most frequently used word from the collections import counter# Most_common (x) x represents the number of print (Counter (txt_list). Most_common (6)) #-----------Output----------------------# [(' More ', 3), (' and ', 3), (' Python ', 2), (' is ', 1), (' A ', 1), (' Programming ', 1)]#------------------------------------# 11. Column Out all uppercase words title_words_list=[]for i in Txt_list:if i.istitle (): Title_words_list.append (i) # Get the Redo dictionary title_words_dict =set (title_words_list) print (title_words_list) print (title_words_dict) #-----------Output----------------------# [' Python ', ' You ', ' python ', ' learn ', ' python ... ' # {' Python: ', ' learn ', 'Python ', ' You '}#------------------------------------# 12. To be Continued ... 

[Python Study Notes] string processing tips (continuous update)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.