Python path [Article 2]: Python basics, python Article 2

Source: Internet
Author: User
Tags printable characters

Python path [Article 2]: Python basics, python Article 2
For Python, everything is an object, and objects are created based on classes.

Therefore, the following values are all objects: "zhurui", 22, ['beijing', 'shanghai', 'shenzhen'], and are generated based on different classes.

 

I. Basic data type number (int)

For example, 21, 45, and 56

Each integer has the following functions:

 

1-int 2 converts a string to a number 3 Example: 4 a = "123" 5 print (type (a), a) 6 output result: 7 >>> a = "123" 8 >>> print (type (a), a) 9 <class 'str'> 12310 11 B = int () 12 print (type (B), B) 13 14 output results: 15 >>> B = int (a) 16 >>> print (type (B), B) 17 <class 'int'> 12318 19 num = "0022" 20 v = int (num, base = 16) 21 print (v) 22 23 output results: 24 >>> num = "0022" 25 >>> v = int (num, base = 16) 26 >>> print (v) 27 3428 29-bit_length30 # binary of the current number, expressed with at least n digits 31 age = 2232 v = age. bit_length () 33 print (v) 34 35 output: 36 >>> age = 2237 >>> v = age. bit_length () 38 >>> print (v) 39 5
String (str)

1. name. capitalize () # uppercase letters

Example:

>>> test = "zhUrui">>> v = test.capitalize()>>> print(v)Zhurui

 

2. name. casefold () # All the smaller writes, casefold is more awesome, and many unknown small writes

Example:

>>> test = "zhUrui">>> v1 = test.casefold()>>> print(v1)zhurui>>> v2 = test.lower()>>> print(v2)zhurui

 

3. name. center () # Set the width and center the content

Name. ljust () # Set the width of the variable to the left, and fill the other part with the defined padding.

Name. Fill ust () # Set the width of the variable to the right, and fill the other part with the defined padding.

Name. zfill () # Set the width. The default variable is to the right. Fill the remaining part with the zfill-specific fill character "000 ".

>>> Test = "zhurui" >>> v = test. center (20, "medium") >>> explanation in zhurui in middle of print (v): #20 # * blank unknown filling, one character, optional output results: ############################### ############>> test = "zhurui" >>> v = test. ljust (20, "*") # Place the ljust variable to the left, and fill the other part with *> print (v) zhurui **************######################### ##########>> test = "zhurui" >>> v = test. rjust (20, "*") # The rjust variable is right-aligned, and the other part is filled with *> print (v) *************** zhurui
>>> Test = "zhurui" >>> v = test. zfill (20) # zfill can only be used for 000 filling >>> print (v) 00000000000000 zhurui

 

4. name. count () # search for sub-sequences in strings

>>> test = "Zhuruizhuruiru">>> v = test.count('ru')>>> print(v)3>>> v = test.count('z')>>> print(v)1#########################################>>> test = "Zhuruizhuruiru">>> v = test.count('ru', 5, 6)>>> print(v)0

 

5. name. encode () # encode the string into bytes format

6. name. decode ()

 

7. name. endswith ("ui") # judge whether the string ends with the ui

Name. startswith ('ui') # determine whether the string starts with the ui

>>> test = "zhurui">>> v = test.endswith('ui')>>> print(v)True>>> v = test.startswith('ui')>>> print(v)False

 

8. "Zhu \ tRui". expandtabs (10) # output 'zhu Rui 'to convert \ t to multiple long spaces.

>>> Test = "Zhu \ tRui" >>> v = test. expandtabs (10) >>> print (v) zhu Rui ################################# test = "username \ tpassword \ temail \ nzhurui \ t123456 \ t24731701@qq.com \ nzhurui \ t123456 \ t24731701@qq.com \ nzhurui \ t123456 \ t24731701@qq.com "v = test. expandtabs (20) print (v) output result: C: \ Python35 \ python3.exe C:/Users/ZR/PycharmProjects/python full stack development/day1/logging. pyusername password emailzhurui 123456 24731701@qq.comzhurui 123456 24731701@qq.comzhurui 123456 24731701@qq.com

9. name. find (A) # find the first one from the start, and obtain the index. If no index is found,-1 is returned.

##> Or> =
#-1 not found
>>> Test = "William" >>> v = test. find ('am') >>> print (v) 5 >>> v = test. find ('T') # find the "t" character in the variable, >>> print (v)-1 ## No, negative 1 is returned

 

10. name. index ('A') # No. An error is returned.

>>> Test = "William" >>> v = test. index ('A') >>> print (v) 5 >>> v = test. index ('8') # Check whether Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: substring not found

 

11. name. format () # format and replace the placeholder in a string with the specified value.

>>> test = 'i am {name}, age {a}'>>> print(test)i am {name}, age {a}>>> v = test.format(name='william',a=22)>>> print(v)i am william, age 22

 

 

>>> test = 'i am {0},age {1}'>>> print(test)i am {0},age {1}>>> v = test.format('william', 22)>>> print(v)i am william,age 22

 

12. name. format_map () # format, input value {"name": 'william', "a": 22}

>>> test = 'i am {name}, age {a}'>>> v1 = test.format(name='zhurui',a=22)>>> v2 = test.format_map({"name":'zhurui', "a": 19})>>> print(v1)i am zhurui, age 22>>> print(v2)i am zhurui, age 19

13. name. isalnum () # Whether the string contains only letters and numbers

>>> test = "234">>> v = test.isalnum()>>> print(v)True

 

14. name. isalpha () # whether it is a letter or a Chinese character

>>> Test = "asfdge242" >>> v = test. isalpha () >>> print (v) False >>> test = "William" >>> v = test. isalpha () >>> print (v) True

15. Determine whether the input is a number.

>>> Test = "2" #1, ② >>> v1 = test. isdecimal # judge decimal places> v2 = test. isdigit () >>> v3 = test. isnumeric () # determine Chinese numbers, such as "2"> print (v1, v2, v3) <built-in method isdecimal of str object at 0x00000201FE440AB0> False True

 

16. name. isprintable () # determine whether there are non-printable characters

\ T Tab
\ N line feed
>>> Test = "qepoetewt \ tfdfde" >>> v = test. isprintable () >>> print (v) False >>>> test = "qepoetewtfdfde" >>> v = test. isprintable () >>> print (v) True

 

17. name. isspace () # determine whether all spaces are used

>>> test = "">>> v = test.isspace()>>> print(v)False>>> test = " ">>> v = test.isspace()>>> print(v)True

 

18. name. istitle () # determine whether it is a title. The first letter must be capitalized.

>>> Test = "Return True if all cased characters in S are uppercase" >>> v1 = test. istitle () >>> print (v1) False >>> v2 = test. title () # capital the first letter of the string >>> print (v2) Return True If All Cased Characters In S Are Uppercase >>>> v3 = v2.istitle () >>> print (v3) true

 

19. ***** name. join () # concatenate each element in the string according to the specified separator (five star key parameter)

>>> Test = "become CEO ">>> print (test) becomes CEO Bai fumei >>> v = '_'. join (test) >>> print (v) outbound _ Ren _ C_E_O _ _ white _ rich _ >>>

 

20. name. islower () # determine whether all are case-insensitive and convert to case-insensitive.

>>> Test = "William" >>> v1 = test. islower () # determine whether all data is in lowercase >>> v2 = test. lower () # convert the variable to lowercase >>> print (v1, v2) false william >>> ################################### >>> test = "William" >>> v1 = test. isupper () # determine whether all data is in uppercase >>> v2 = test. upper () # convert the variable to uppercase >>> print (v1, v2) False WILLIAM

 

21. Remove a specified string and match the string at most.

>>> Test = 'xa '>>> v1 = test. isupper () >>> v = test. lstrip ("xa") >>> print (v) >>> v = test. rstrip ("92 exxxexxa") >>> print (v) >>> v = test. strip ("xa") >>> print (v) #################################### test. lstrip () # test. rstrip () # test. strip () # Remove left and right spaces # v = test. lstrip () # v = test. rstrip () # v = test. strip () # print (v) # print (test) # Remove \ t \ n # v = test. lstrip () # v = test. rstrip () # v = test. strip () # print (v)

 

22. Replace the correspondence

>>> test = "aeiou">>> test1 = "12345">>> v = "asidufkasd;fiuadkf;adfkjalsdjf">>> m = str.maketrans("aeiou", "12345")>>> new_v = v.translate(m)>>> print(new_v)1s3d5fk1sd;f351dkf;1dfkj1lsdjf

 

23. name. partition () # split into three parts

>>> test = "testegerwerwegwewe">>> v = test.partition('s')>>> print(v)('te', 's', 'tegerwerwegwewe')>>> v = test.rpartition('s')>>> print(v)('te', 's', 'tegerwerwegwewe')>>>

 

24. name. split () # The number is specified.

>>> test = "sagesgegessress">>> v = test.split('s',2)>>> print(v)['', 'age', 'gegessress']>>>

 

25. segmentation. It can only be set to true or false. whether to retain line breaks.

>>> test = "fwerwerdf\frweqnndasfq\fnaqewrwe">>> v = test.splitlines(False)>>> print(v)['fwerwerdf', 'rweqnndasfq', 'naqewrwe']

 

26. Start with xxx and end with xx

>>> test = "backend 1.2.3.4">>> v = test.startswith('a')>>> print(v)False>>> test.endswith('a')False

 

27. name. swapcase () # case-insensitive Conversion

>>> test = "WiiLiAm">>> v = test.swapcase()>>> print(v)wIIlIaM

 

28. name. isidentifier () # letters, numbers, underlines: identifier def class

>>> a = "def">>> v = a.isidentifier()>>> print(v)True

 

29. name. replace () # replace the specified string with the specified string. The replacement function is equivalent to sed.

>>> test = "williamwilliamwilliam">>> v = test.replace("am", "bbb")>>> print(v)willibbbwillibbbwillibbb>>> v = test.replace("am", "bbb",2)>>> print(v)willibbbwillibbbwilliam>>>

 

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.