python-07. various types of objects

Source: Internet
Author: User
Tags delete key true true

Basic data types in detail one, str string

1.capitalize string first letter capitalization

  Does not change itself, it generates a new value

1 name = ' Zhangsan ' 2 new_name = name.capitalize () 3 print (name) 4 print (new_name) 5 6 output: 7 zhangsan8 Zhangsan

2,casefold all uppercase to lowercase, Casefold can make any language uppercase lowercase

1 name = ' Sunchenguang ' 2 new_name = name.casefold () 3 print (name) 4 print (new_name) 5 6 output: 7 SunChenGuang8 Sunchenguang

3,Lower all uppercase to lowercase, lower can only change the English

1 name = ' Sunchenguang ' 2 new_name = name.lower () 3 print (name) 4 print (new_name) 5 6 output: 7 SunChenGuang8 Sunchenguang

4, Center Text Center

# parameter 1: indicates total length
# parameter 2: whitespace filled characters (length 1)

1 name = ' Sunchenguang ' 2 new_name = name.center (' # ') 3 print (name) 4 print (new_name) 5 6 output: 7 SunChenGuang8 # # # #SunChenG uang#####     

5.Count counts the number of occurrences of incoming values in a string

# parameter 1: the value to look for (sub-sequence)
# parameter 2: starting position (index)
# parameter 3: End position (index)

1 neme = ' Sunchenguang ' 2 v1 = Neme.count (' g ') 3 v2 = Neme.count (' g ', 0,6) 4 v3 = Neme.count (' g ', 3,9) 5 print (neme) 6 print (v1) 7 Print (v2) 8 print (v3) 9 10 output content: SunChenGuang12 313 114 2

6,EndsWith whether to end with xx, The return value is TRUE or False

1 name = ' Sunchegnuang ' 2 new_name = Name.endswith (' Ong ') 3 print (name) 4 print (new_name) 5 6 output content: 7 Sunchenguang8 True

7,StartsWith whether to start with xx, The return value is TRUE or False

1 name = ' Sunchenguang ' 2 new_name = Name.startswith (' gu ') 3 print (name) 4 print (new_name) 5 6 output content: 7 Sunchenguang8 True

8,expandtabs find the tab \ t, replace (contains the preceding Value)

# parameter is 8 by default

1 name = ' Sun\tchen\tguang\nsun\tchen\tlguang ' 2 new_name = name.expandtabs (ten) 3 print (name) 4 print (new_name) 5  6 output Content: 7 Sun    Chen    Guang 8 Sun Chen Guang 9 Sun       Chen      guang10 Sun Chen Guang

9. Find the index position of the specified sub-sequence

Find: There is a return index position, there is no return-1

Index: There is a return index position, there is no error

1 name = ' Sunchegnaung ' 2 v1 = name.find (' c ') 3 v2 = Name.index (' s ') 4 v3 = name.find (' x ') 5 # v4 = Name.index (' x ') 6 Print (v1, V2,v3) 7 8 Output content: 9 3 3-1

10. String Formatting

%: According to percent sign

1 v1 = ' name:%s; age:%s; gender:%s ' 2 new_v1 = v1% (' Sun Chenguang ', 20, ' male ') 3 print (new_v1) 4 5 output: 6 name: Sun Chenguang; age: 20; gender: Male

Format: according to Subscript

1 v2 = ' name: {0}; age: {1}; gender: {2} ' 2 new_v2 = V2.format (' Sun Chenguang ', 20, ' male ') 3 print (new_v2) 4 5 output: 6 name: Sun Chenguang; age: 20; gender: Male

Format_map: based on key value pairs

1 v3 = ' name: {name}; age: {gender} ' 2 new_v3 = v3.format_map ({' name ': ' Zhang San ', ' ages ': ' gender ': ' male '}) 3 print (new_v3) 4 5 Output content: 6 name: Zhang san; age: 20; gender: Male

11,isalnum judge whether it is a number, letter, Chinese characters

# numbers, letters, Kanji True

# Special Symbol False

1 name1 = ' utility bill ' 2 name2 = ' Utility bill ' 3 v1 = name1.isalnum () 4 v2 = name2.isalnum () 5 Print (v1,v2) 6 7 output content: 8 True False

12. Judging whether it is a number

isdecimal # ' 123 '

isdigit # ' 123 ', ' ② '

isnumeric # ' 123 ', ' two ', ' ② '

1 NUM1 = "123" 2 num2 = "②" 3 num3 = "two" 4 V1 = num1.isdecimal ()    # ' 123 ' 5 v2 = num2.isdigit ()      # ' 123 ', ' ② ' 6 v3 = Num3.isnumeric ()    # ' 123 ', ' two ', ' ② ' 7 print (v1,v2,v3) 8  9 Output Content: true True    

13,Isidentifier Determine whether the variable name is legitimate

1 name1 = ' guoshaolong1 ' 2 name2 = ' 1guoshaolong ' 3 v1 = name1.isidentifier () 4 v2 = name2.isidentifier () 5 Print (v1,v2) 6 7 output Content: 8 True False

14,islower judge whether all lowercase

1 name1 = ' Zhangsan ' 2 name2 = ' Zhangsan ' 3 v1 = name1.islower ()    #判断是否全部是小写4 v2 = name2.islower ()    #判断是否全部是小写5 Print ( V1,v2) 6 7 Output content: 8 True False

  Isupper Determine if all are uppercase

1 Name3 = ' Zhangsan ' 2 name4 = ' Zhangsan ' 3 v3 = name3.isupper ()    #判断是否全部是大写4 v4 = name4.isupper ()    #判断是否全部是大写5 Print ( V3,v4) 6 7 Output content: 8 True False

15.Upper All caps

1 name = ' Sunchenguang ' 2 new_name = name.upper () 3 print (name) 4 print (new_name) 5 6 output: 7 Sunchenguang8 Zhangsan

Lower All lowercase

1 name = ' Zhangsan ' 2 new_name = name.lower () 3 print (name) 4 print (new_name) 5 6 output: 7 ZHANGSAN8 Zhangsan

16. does theisprintable contain the implied \ t

#不包含返回True, contains the return false

1 name1 = ' Zhangsan ' 2 name2 = ' Zhang\nsan ' 3 v1 = name1.isprintable () 4 v2 = name2.isprintable () 5 Print (v1,v2) 6 7 output: 8 Tru E False

17,isspace are all spaces

1 name = '  2 v = name.isspace () 3 Print (v) 4 5 output content: 6 True

18. element concatenation (element String) importance five Stars * * * *

1 name = ' Zhangsan ' 2 v1 = "_". Join (name) 3 Print (v1) 4  5 name_list = [' Zhang San ', ' John Doe ', ' Harry '] 6 v2 = "*". Join (name_list) 7 PR int (v2) 8  9 output content: z_h_a_n_g_s_a_n11 Zhang San * John Doe * Harry

19, left and right fill

   Center text centered (see 4th)

  rjust Right padding

  Ljust left padding

1 name = ' Zhangsan ' 2 v1 = name.rjust ("*")    #右填充3 v2 = name.ljust ("*")    #左填充4 print (v1) 5 print (v2) 6 7 output Content: 8 *********zhangsan9 zhangsan*********

20. Corresponding relationship + translation

1 m = Str.maketrans (' aeiou ', ' 12345 ') 2 name = ' ABCDEFGHIJKLMNOPQRSTUVWXYZ ' 3 v = name.translate (M) 4 print (v) 5 6 output content: 7 1BCD2 Fgh3jklmn4pqrst5vwxyz

21,partition segmentation, Preserving the elements of the Division

1 name = ' Zhang.san.si '
2 v = name.partition ('. ') 3 Print (v) 4 5 output content: 6 (' Zhang ', '. ', ' san.si ')

22. Replace replacement

1 name = ' zhang.san.si ' 2 v1 = name.replace ('. ', ' * ') 3 v2 = name.replace ('. ', ' * ', 1) 4 print (v1) 5 print (v2) 6 7 output: 8 zhang*s An*si9 zhang*san.si

23,strip removal of blank, \n,\t

1 name = '  zhangsan\t ' 2 v = name.strip () 3 print (name) 4 print (v) 5 6 output content: 7   Zhangsan   8 Zhangsan

24.swapcase Case Conversion

1 name = ' Zhangsan ' 2 v = name.swapcase () 3 print (name) 4 print (v) 5 6 output content: 7 ZhangSan8 Zhangsan

25,Zfill to the front padding 0

1 name = ' Zhangsan ' 2 v = name.zfill (3) Print (v) 4 5 output: 6 000000000zhangsan

26. Convertencode to bytes

1 name = ' Zhang San ' 2 v1 = Name.encode (encoding= ' utf-8 ') 3 v2 = Name.encode (encoding= ' GBK ') 4 print (v1) 5 print (v2) 6 7 output: 8 B ' \xe9 \x83\xad\xe5\xb0\x91\xe9\xbe\x99 ' 9 B ' \xb9\xf9\xc9\xd9\xc1\xfa '
two int integers

1,bit_length The binary representation of the current integer, the minimum number of digits

1 age1 = 1 2 age2 = 3 Age3 = 4 print (age1.bit_length ()) 5 print (age2.bit_length ()) 6 print (age3.bit_length ()) 7
   
    8 Output content: 9 110 411 7
   

2.to_bytes Gets the byte representation of the current data

1 Age = 202 V1 = age.to_bytes (10,byteorder= ' Big ') 3 v2 = age.to_bytes (10,byteorder= ' Little ') 4 print (v1) 5 print (v2) 6 7 output Content: 8 B ' \x00\x00\x00\x00\x00\x00\x00\x00\x00\x14 ' 9 B ' \x14\x00\x00\x00\x00\x00\x00\x00\x00\x00 '
third, BOOL Boolean value

1. Only true or False

1 v1 = 0 2 v2 = 1 3 v3 =-1 4 v4 = ' 5 v5 = [] 6 print (bool (v1)) 7 print (bool (v2)) 8 print (bool (v3)) 9 print (bool (v4)) 10 Print (bool (v5)) 11 12 output content: False14 True15 True16 False17 False
Iv. List of lists

1,Append Additional

1 user_list = [' eldest ', ' Dick ', ' old three ', ' old four ']2 user_list.append (' old five ') 3 print (user_list) 4 5 output content: 6 [' eldest ', ' Dick ', ' old three ', ' old four ', ' old five ']

2.Clear Clear

1 user_list = [' eldest ', ' Dick ', ' old three ', ' old four ']2 user_list.clear () 3 Print (user_list) 4 5 output content: 6 []

3. copy copy (light Copy)

1 user_list = [' eldest ', ' Dick ', ' old three ', ' old four ']2 v = user_list.copy () 3 print (user_list) 4 print (v) 5 6 output: 7 [' eldest brother ', ' Dick ', ' old three ', ' old four ']8 [' eldest ', ' Dick ', ' old three ', ' old four ']

4. Count counts

1 user_list = [' eldest ', ' Dick ', ' old three ', ' old four ', ' old three ']2 v = user_list.count (' old three ') 3 print (user_list) 4 print (v) 5 6 output: 7 [' eldest ', ' Dick ', ' old Three ', ' Old four ', ' old three ']8 2

5,extend extension of the original list

1 user_list = [' eldest ', ' Dick ', ' old three ', ' old four ']2 user_list.extend ([' Guo Shaolong ', ' Little Dragon ']) 3 print (user_list) 4 5 output: 6 [' eldest ', ' Dick ', ' old three ', ' old four ', ' Guo Shaolong ', ' Little Dragon ']

6, Index to find the elements, no errors

1 user_list = [' eldest ', ' Dick ', ' old three ', ' old four ']2 v = user_list.index (' boss ') 3 Print (v) 4 5 output content: 6 0

7.Pop Delete and get element-delete according to index

1 user_list = [' eldest ', ' Dick ', ' old three ', ' old four ']2 v = user_list.pop (1) 3 print (v) 4 print (user_list) 5 6 output: 7 old 28 [' eldest brother ', ' old three ', ' old four ']

8, Remove delete-based on the value of the deletion

1 user_list = [' eldest ', ' Dick ', ' old three ', ' old four ']2 user_list.remove (' Dick ') 3 print (user_list) 4 5 output: 6 [' eldest ', ' old three ', ' old four ']

9,Reverse Flip

1 user_list = [' eldest ', ' Dick ', ' old three ', ' old four ']2 user_list.reverse () 3 Print (user_list) 4 5 output content: 6 [' old four ', ' old three ', ' Dick ', ' boss ']

10. Sort Order

1 positive order: 2 nums = [5,4,3,2,6,10,7] 3 nums.sort () 4 print (nums) 5  6 output: 7 [2, 3, 4, 5, 6, 7, ten] 8  9 10 Reverse: nums = [5,4,3,2,6,10,7]12 nums.sort (reverse=true) Print (nums) 14 15 output content: 16 [10, 7, 6, 5, 4, 3, 2]

11. Supplemental content:Range

1, Output 1-10

1 for I in range (1,11): 2     print (i) 3  4 output content: 5 1 6 2 7 3 8 4 9 510 611 712 813 914 10

2, output 1-10 Odd

1 for I in range (1,11,2): 2     print (i) 3 4 output content: 5 16 37 58 79 9

3, Reverse Output 10-1

1 for I in range (10,0,-1): 2     print (i) 3  4 output content: 5 10 6 9 7 8 8 7 9 610 511 412 313 214 1

4. Output List Contents + Serial number

1 name = [' Zhang San ', ' John Doe ', ' Harry ']2 for I in range (0,len (name)): 3     print (i+1,name[i]) 4 5 output: 6 1 Sheets 37 2 Li 48 3 Harry

12. Supplemental content:Enumerate generate an extra column of sequential numbers

1 name = [' Zhang San ', ' John Doe ', ' Harry ']2 for I,ele in enumerate (name,1): 3     print (i,ele) 4 5 output: 6 1 Sheets 37 2 Li 48 3 Harry
V. tuple: tuples (non-modifiable lists; immutable types)

# tuples If there is only one value, you need to add a comma at the end

1,Count Gets the number

1 user_tuple = (' Zhang San ', ' John Doe ', ' Harry ', ' Zhang San ',) 2 v = user_tuple.count (' Zhang San ') 3 Print (v) 4 5 output content: 6 2

2. Index Gets the first indexed position of a value

1 user_tuple = (' Zhang San ', ' John Doe ', ' Harry ', ' Zhang San ',) 2 v = user_tuple.index (' Zhang San ') 3 Print (v) 4 5 output content: 6 0
Vi. Dict: Dictionary

1.Clear Clear

1 dic = {' K1 ': ' v1 ', ' K2 ': ' v2 '}2 dic.clear () 3 Print (dic) 4 5 output content: 6 {}

2. Copy light copy

1 dic = {' K1 ': ' v1 ', ' K2 ': ' v2 '}2 v = dic.copy () 3 print (dic) 4 print (v) 5 6 output content: 7 {' K2 ': ' v2 ', ' k1 ': ' v1 '}8 ' K2 ': ' v2 ', ' K1 ': ' V1 '}

3, Get the specified value according to key; no error is present

1 dic = {' K1 ': ' v1 ', ' K2 ': ' v2 '}2 v1 = dic.get (' k111 ', ' 111 ') 3 Print (v1) 4 5 output content: 6 111

4,pop Delete and get the corresponding value values

1 dic = {' K1 ': ' v1 ', ' K2 ': ' v2 '}2 v1 = dic.pop (' K1 ') 3 Print (v1) 4 5 output content: 6 v1

5,popitem Random Delete key value pairs, and get to delete the key value

1 dic = {' K1 ': ' v1 ', ' K2 ': ' v2 '}2 v = dic.popitem () 3 print (v) 4 print (dic) 5 6 output content: 7 (' K2 ', ' v2 ') 8 {' K1 ': ' v1 '}

6,SetDefault increase, If there is no operation

1 dic = {' K1 ': ' v1 ', ' K2 ': ' v2 '}2 dic.setdefault (' K3 ', ' v3 ') 3 print (dic) 4 5 output content: 6 {' k1 ': ' v1 ', ' K2 ': ' v2 ', ' K3 ': ' v3 '}

7.Update Batch Add or modify

1 dic = {' K1 ': ' v1 ', ' K2 ': ' v2 '}2 dic.update ({' K3 ': ' v3 ', ' K1 ': ' v33333 '}) 3 print (dic) 4 5 output content: 6 {' K2 ': ' v2 ', ' K1 ': ' v33333 ', ' K ' 3 ': ' v3 '}
set, set (non-repeating list; mutable Type)

1, S1 exist, S2 does not exist

1 S1 = {"a", ' b ', ' C ', ' d ', ' e '} 2 s2 = {"a", ' b ', ' C ', ' d '} 3 v = s1.difference (s2) 4 print (v) 5 # # # # # # # # # s1, s2, Then Copy 6 s1.difference_update (s2) 7 Print (s1) 8  9 output content: {' e '}11 {' e '}

2, S2 exist, S1 does not exist

1 S1 = {"a", ' b ', ' d ', ' e '}2 s2 = {"a", ' b ', ' C ', ' d '}3 v = s2.difference (s1) 4 print (v) 5 6 output content: 7 {' c '}

python-07. various types of objects

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.