Python Text Processing (1)

Source: Internet
Author: User

Solution for processing one character each time: Create a list [python] thestring = 'abcdefg' thelist = list (thestring) print thelist [python] [python] result [python] ['A ', 'B', 'C', 'D', 'E', 'F ', 'G'] use the for statement to traverse [python] thestring = 'abcdefg' for c in thestring: print c using the list derivation formula. (Note that the ord statement is used to convert the character to the character value, for example, convert a to 97) [python] thestring = 'abcdefg' results = map (ord, thestring) print results: to obtain a set of all characters in a string, call sets. set [python] import sets magic_chars = sets. set ('a Bracadabra ') poppins_chars = sets. set ('supercalifragilisticexpialidocious ') print ''. how to convert the join (magic_chars & poppins_chars) character to the character value: Use the ord and chr [python] print ord ('A') print chr (97) Functions) test whether an object is a class string (whether the object has a string behavior pattern) solution: Use isinstance and basestring to check [python] def isAString (anobj): return isinstance (anobj, basestring) anobj = 'abcde' print isAString (anobj) otherobj = list (anobj) print isAString (otherobj) string alignment (Left-aligned, center-aligned, and right-aligned) solution: Use the string object's ljust, must, and center. The parameter specifies the width [python] print '|', 'hs '. ljust (20), '|', 'hs '. must ust (20), '|', 'H '. center (20), '|' discussion: It can be printed with other characters without spaces. You only need to add the second parameter [python] print 'H '. center (20, '+') removes spaces at both ends of the string. Solution: Use the string object's lstrip, rstrip, and strip [python] x = 'hes' print '|', x. lstrip (), '|', x. rstrip (), '|', x. strip (), '|' merge strings. join [python] x = ['I', 'love', 'python'] largestring = ''using the string operator ''. join (x) Print largestring Similarly, using the most basic % can also achieve this effect [python] x = ('I', 'love', 'python ') largestring = '% s! '% X print largestring: Of course, the + operations using strings seem to be more concise, but in python, strings cannot be changed, any change will create a copy of the current string. When a large number of short strings are added, the created copy is proportional to its square, in this case, using the join method is a necessary choice. When you need to add additional content to the new string you created, it is more convenient to use %. How to reverse a string by character or word: Use the slicing method with step-1 [python] astring = 'I Love python' revchars = astring [:: -1] print revchars result nohtyP evoL I is reversed by words, you need to create a word list and reverse the list, finally, use join to merge [python] astring = 'I Love python' revwords = ''. join (astring. split () [:-1]) print revwords result Python Love I wants to reverse word by word but does not want to change the original space, use a regular expression to separate the original string [python] import re astring = 'I Love python' revwords = ''. join (re. split (R' (\ s +) ', astring) [:-1]) print revw Ords result Python Love I check whether the string contains characters in a character set. solution: the simplest method is as follows: [python] def containAny (seq, aset): for c in seq: if c in aset: return True return False seq = 'abc' aset = 'hjkyuia 'print containAny (seq, aset) can also use methods based on the itertools module of the standard library, however, in essence, the same method [python] import itertools def containAny (seq, aset): for item in itertools. ifilter (aset. _ contains __, seq): return True return False seq = 'abc' aset = 'ghjka 'print contai NAny (seq, aset) checks whether a string is a text or binary solution: no precise algorithms are available, but some heuristic methods can be used, if the string contains a null value or a high value of more than 30% is set to 1 or a strange control code, it is considered that this data segment is a binary data case-sensitive (case-sensitive conversion) solution: using the upper and lower methods is relatively simple, but generally the capitalize and title methods [python] view plaincopyprint? Print 'one tWo thrEe '. capitalize () print 'one tWo thrEe'. title () Result one two three one Two Three

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: 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.