Python Learning (vii) List/dictionary merge, Boolean, swap variable value, list conversion string join and Split

Source: Internet
Author: User
Tags string format

1. List Merge
A=[]b=[12,23]a.insert (0,'sdv')# add element at specified position A.append ('we')# add element a.extend (b)  # Merge List  Print(a)

A.exend (b) Merges the B list contents into the A list and outputs the result:

['sdv''we', 12, 23]
2. Dictionary Merging
a={"K1":"v1"}b={'K2 ':'v2'}a.update (b)  # Add the elements of the B dictionary to the A dictionary  Print(a)

A.update (b) merges the contents of the B dictionary into the A dictionary and outputs the result:

{'K1'v1'K2 ' v2 '}
3. Boolean type
Non-NULL is true, not 0 is true
If it's not empty, it's true, empty is false.
As long as not 0 is true, 0 is False
 name = input ( "  Please enter your name:  "  ). Strip ()  if  Span style= "COLOR: #000000" > name:  print  (  Enter correct   ")  #   else  :  print  ("   input cannot be empty   ") 
A = []
b ={}
f = tuple () e = " The above list, dictionary, int type, tuple, and string are all empty, then return is false
4, the value of the Exchange variable 1, the value of Exchange two variables (a)
A = 1= 2= b  # Exchange values for two variables print(A, B)

The output is: 2,1

2. Exchange value of two variables (two)
A = 1= 2= a+= A-bprint(A, a)
3. Exchange values of three variables
A = 1= 2= 3# swap the value of three variables print(a,b,c)

Output Result: 3,2,1

5. Method of String
A = ' python1d '
Print (A.isdigit ()) #是否全部都是数字
Print (A.isupper ()) #是否全部都是大写字母
Print (A.islower ()) #是否全部都是小写字母
Print (A.isalpha ()) #是否全都是汉字或者字母
Print (A.isalnum ()) #是否全都是汉字或者字母或者数字 (as long as no special word nonalphanumeric returns True) 6, introduce the string module
6. Introduction of the String module
Import   stringprint(string.ascii_lowercase)  # All lowercase letters Print # All uppercase Letters Print # all the numbers Print # all uppercase and lowercase letters Print # all the special characters

The output is:

abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789  abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz! " #$%& ' () *+,-./:;<=>[email protected][\]^_ ' {|} ~
7, List conversion string Connection 1, inappropriate way
L = ['a','b','c',' D ' ]SL=str (l)print(type (l), L)print(type (SL), SL) Print (Sl[0])  # This cast will default the brackets in the original list to be part of the string.
2. Correct connection method
". Join (') #可以把list变成字符串; Connect the elements in the list with a string
L = ['Emily','Liu','jia'   '# connect the elements of L with an empty string into the string print(res)

The output is:

Emilyliujia
You can also customize which format to connect
L = ['Emily','Liu','jia'  ='--'. Join (L)  # with--connect the elements of L to string print (RES)

The output is:

Emily--liu--jia
3. Connection Cycle

(". Join this method can be connected as long as it can be looped back.")

Import', '. Join (string.ascii_lowercase) print (   Res

The output is:

A,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z
8. Split string
names ='emily,niuhanyang,liujia,zch'name_list=names.split (' a '  # splits the string according to the letter A, or splits the string according to other strings print(name_list)

The output is:

['emily,niuh'ny'ng,liuji' ' , Zch ']
The list is returned after the split
This method can only be used as a string
If a string is split, the string does not have that particular string, then the returned list is an element of all strings as a list element
9, the string format Method 1
user ='Emily'Sex='female' Age=28Addr='Chengdu' Money='200w'Cars='1w Units'words='INSERT INTO user values ("%s", "%s", "%s", "%s", "%s", "%s");'% (User,sex,age,addr,money,cars)#order can not be chaotic, must be in order toPrint(words)

The output is:

Insert into user values ("Emily", "female", "28", "Chengdu", "200w", "1w Station")
Mode 2
user ='Emily'Sex='female' Age=28Addr='Chengdu' Money='200w'Cars='1w Units'SQL='INSERT into user values ({name},{sex},{age},{addr},{qian},{che})'New_sql=sql.format (age=age,che=cars,name=user,addr=addr,qian=money,sex=sex)Print(New_sql)

A SQL is defined, and the elements in this SQL correspond to the input elements and can be used in order to

Insert into user values (Emily, Female, 28, Chengdu, 200w,1w station)
10. String input Characters
Print (' Welcome to login '. Center (+'*'))  # make ' welcome login ' centered, not enough characters to be padded

Enter as:

Welcome to login ***********************

Python Learning (vii) List/dictionary merge, Boolean, swap variable value, list conversion string join and Split

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