Python Learning Notes

Source: Internet
Author: User

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

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.