One. Text
1. Processing one character at a time
1) List
2) Import Sets
>>> Import Sets
>>> magic_chars=sets. Set (' Adboieihoejoewjo ')
>>> enchar=sets. Set (' EIJOWJ ')
>>> ". Join (Magic_chars & Enchar) #集合的交集
' Ijeow '
>>>
2. Conversion between character and character values
>>> Print Ord (a)
Traceback (most recent):
File "<stdin>", line 1, in <module>
Nameerror:name ' A ' is not defined
>>> Print ord (' a ')
97
>>> Print Chr (' 99 ')
Traceback (most recent):
File "<stdin>", line 1, in <module>
Typeerror:an Integer is required
>>> Print Chr (99)
C
>>> Print repr (str (99)
...
... )
' 99 '
3. Test whether an object is a class string
>>> print isinstance (abc,basestring)
Traceback (most recent):
File "<stdin>", line 1, in <module>
Nameerror:name ' abc ' is not defined
>>> print isinstance (' abc ', basestring)
True
4. String alignment
>>> ' ABC ' Center (20, ' __ ')
Traceback (most recent):
File "<stdin>", line 1, in <module>
Typeerror:center () argument 2 must is char, not str
>>>
>>> print ' ABC '. Center (20, ' __ ')
Traceback (most recent):
File "<stdin>", line 1, in <module>
Typeerror:center () argument 2 must is char, not str
>>> ' ABC ' Center (20, ' _ ')
' ________abc_________ '
>>>
>>> ' AAA '. Ljust (20, ' @ ')
' [Email protected]@@@@@@@@@@@@@@@@ '
>>> ' BBB '. Rjust (20, ' @ ')
' @bbb '
>>>
5. Remove spaces at both ends of the string
>>> x= ' P '
>>> X.ltrip ()
Traceback (most recent):
File "<stdin>", line 1, in <module>
Attributeerror: ' str ' object has no attribute ' Ltrip '
>>> X.lstrip ()
' P '
>>> X.rstrip ()
' P '
>>> X,strip ()
Traceback (most recent):
File "<stdin>", line 1, in <module>
Nameerror:name ' strip ' is not defined
>>> X.strip ()
' P '
>>>
6. Merging strings
7. Invert the string in successive
#正则匹配 Match Spaces
astring= ' Hello World '
Import re
#split (R ' () ', string)
Rewords=re.split (R ' (\s+) ', astring)
'. Join (Rewords)
8. Check if a character in a character set is included in the string
9. Use of methods to simplify the translate of strings
10. Filter characters in a string that are not part of a specified collection
11.python of Itertools
This article is from the "Wang Nimei Adult Road" blog, reprint please contact the author!
Python Learning Notes