Strings, tuples, and dictionaries
1, String
A string is an immutable sequence of several different unicode characters
1 . String creation
#Single quote string str1 = 'It's going to rain. My mother wants to get married. Let him go' str2 = str() #Empty string str3 = str([10,20,30]) #Double quoted string str2 = "It's going to rain. My mother wants to get married. Let him go" #Three quotation mark string multiline string str3 = ''' Good rain knows the season When spring comes ''' str4 = """ Sneak into the night with the wind Moisten things silently ""
2. Get characters
Because it is an immutable sequence, you cannot modify a single character; Any operation to modify the string will produce a new string
str1 = 'It's going to rain. My mother wants to get married. Let him go' print(str1[0],str1[-1]) #Oh, my God
3. String escape
-
Common escape characters
Transfer character explain Transfer character explain \' Single quotation mark \" Double quotation mark \n Line feed \r enter \t tab \\ \ -
Native character
If you do not treat \ as a transfer character in the string, you can use it (read what you see)
str1 = r'c:\wh1803\course\1' print(str1) #c:\wh1803\course\1
4. Sequence general operation
#1 string splicing #For string literals, as long as there are no other characters between two adjacent strings, they will be automatically spliced into one string str1 = 'China and the United States reached a consensus' 'No trade war' print(str1) #China and the United States reached a consensus not to fight a trade war #In other cases, use the + operator to splice strings str1 = 'Hello' str2 = ' Mr. tree' print(str1 + 'world') print(str1 + str2) #2. Duplicate string str1 = 'Woof' * 3 print(str1) #Woof, woof #3. Member operation str1 = 'It's going to rain. My mother wants to get married. Let him go' if 'day' in str1: print('Is a member') else: print('Not a member') #4. String interception (slicing) str1 = '123456' print(str1[0:2]) #'12' print(str1[1:]) #'23456' print(str1[::2]) #'135' print(str1[:]) #'123456' print(str1[::-1]) #'654321' #5. String length print(len(str1))
4 string common functions
4.1 string search and replacement
str1 = 'a fox jumped over the fence'
Method name | explain | Example |
---|---|---|
str.count(sub,start=0,end=len(string)) | Find the number of occurrences of substring sub; start starts with the specified subscript and end s with the subscript | str1.count('f') |
str.find(str, beg=0, end=len(string)) | Check whether the string contains the substring str from left to right. If so, return the subscript; otherwise, return - 1. beg and end are optional parameters that specify the search range | str1.find('fox') |
str.rfind(sub[, start[, end]]) | Check whether the string contains sub substring from right to left, including the subscript of the returned substring; otherwise, return - 1 | str1.rfind('fox') |
str.index(sub[, start[, end]]) | The function is similar to find, but if the substring sub does not exist, an error ValueError will be reported | str1.index('fox') |
str.rindex(sub[, start[, end]]) | Right to left detection, similar to rfind, will report ValueError | str1.rindex('fox') |
str.replace(old, new[, count]) | Returns a new string. The old in the original string is replaced with new. The optional parameter count specifies the number of replacements. | str1.replace('a','many') |
4.2 string separation and combination
Method name | explain | Example |
---|---|---|
str.split([sep[, num=count(sep)]]) | Split the string into a list separated by sep. if num is specified, it can be split up to num times | str1.split(' ') |
str.rsplit([sep[, num=count(sep)]]) | Split from right to left | |
str.partition(seq) | Split the string into a tuple with three elements (string before seq, string after seq, string after seq). | |
str.rpartion(seq) | ditto | |
str.splitlines([keepends]) | Split a string containing multiple lines and return a list for each element. keepends is a True character or non-zero integer indicating that the end of line flag (i.e. newline character) is reserved | |
str.join(seq) | Take the specified string str as the separator to merge all elements (string representation) in the seq object into a new string; seq can be string, list, etc |
4.3 string judgment
method | explain | Example |
---|---|---|
str.isalpha() | Judge whether the string is composed of letters and contains only letters. Whether it returns True or not returns False | str1.isalpha() |
str.isalnum() | Check whether the string is composed of letters and numbers. If both are numbers or letters, return True; otherwise, return False | |
str.isdigit() | Check whether the string is composed of numbers, and the byte type can be detected | |
str.isdecimal() | Detects whether a string is composed of numbers | |
str.isnumeric() | Detect whether the string is composed of numbers. You can detect Chinese numerals: ten | |
str.isspace() | Check whether the string consists of only spaces or tab s | |
str.islower() | Detects whether all alphabetic characters in the string are composed of lowercase letters | |
str.isupper() | Detect whether all alphabetic characters in the string are composed of large letters | |
str.startswith(suffix[, start[, end]]) | Used to determine whether the string starts with the specified substring. If yes, it returns True; otherwise, it returns False. | |
str.endswith(suffix[, start[, end]]) | Used to determine whether the string ends with the specified substring. If yes, it returns True; otherwise, it returns False. |
4.4 string conversion
method | explain | Example |
---|---|---|
str.lower() | String to lowercase | |
str.upper() | Convert string to uppercase | |
str.swapcase() | Swap the upper and lower case letters in the string, convert uppercase to lowercase, and convert lowercase to uppercase. Ignore non alphabetic characters. | |
str.capitalize() | Converts the first character of a string to uppercase and the rest to lowercase | |
str.title() | The first letter of each word in the string is uppercase and the rest is lowercase. | |
str1.center(width,[fillchar]) | The string is displayed in the center with the specified width. If the string cannot fill the width, the string will be filled with the specified character, which is filled with spaces by default | str1.center(80,'*') |
str.ljust(width[, fillchar]) | Displays the string to the left with the specified width | |
str.rjust(width[, fillchar]) | Displays the string to the right with the specified width | |
str.lstrip([chars]) | Remove the specified characters on the left of the string. The default is to remove spaces | |
str.rstrip([chars]) | Remove the specified characters on the left of the string. The default is to remove spaces | |
str.strip([chars]) | Remove the characters specified on both sides of the string. The default is to remove spaces |
4.5 other methods
#1. Convert other types to strings print(str(90)) #'90' print(str([20,30])) #'[20,30]' #2 ord(x) returns the code value corresponding to a character print(ord('a')) #97 print(ord('in')) #20013 #3 chr(x) enter a unicode code and return a corresponding character. print(chr(20013)) #in #eval, execute the string as code age = 1 print(eval('age + 3')) # 4 print(eval('+123')) #123 print(eval('3 + 2')) #5 #There are safety problems eval("__import__('os').system('dir')") #Displays a list of files in the current directory #4 repr(x) returns the String format of an object, which is suitable for machine execution a = [20,30] s1 = repr(a) list1 = eval(s1) list2 = str(a) print(a == list1) print(a == list2)
5. String formatting
-
Format with%
%[flags][width][.precision]typecode flags:For its way,-Align left +Right align(default),0 Indicates filling with 0(For numeric only),The default is to fill with spaces width: Width occupied, in columns .precision: Precision. If there is a decimal point, you can specify several decimal places, which will be rounded typecode: d Convert numerical value to integer display; f Convert numeric values to floating point display s Convert numeric values to string display #Hello, my name is Wang NIMA. I'm 35 years old. I have 5000000.69 print("Hello, my name is%+6s,I this year%d Years old,I have%10.2f" % ('Wang NIMA',35,5000000.687))
-
Format with format
[[fill]align][sign][#][width][.precision][type] fill: Fill character, optional align: Alignment <Align left >Right align ^Center alignment sign: Display symbols,+A positive number shows a positive sign and a negative number shows a symbol;-Positive numbers do not display symbols, while negative numbers display symbols #: 0b 0o 0x will be displayed for 2, 8 and hexadecimal width: width , Thousands separator .precision: accuracy type: s character string d integer f Floating point number tp1 = "I am {}, age {}, {}".format("seven", 18, 'alex') tp2 = "I am {name}, age {age}, really {name}".format(name="seven", age=18) tp3 = "I am {:s}, age {:d}, money {:.0f}".format("seven", 18, 88888.1) print(tp1) #I am seven, age 18, alex print(tp2) #I am seven, age 18, really seven print(tp3) #I am seven, age 18, money 88888
2, Byte
The most important feature of Python 3 is that it makes a clearer distinction between text and binary data. Text is always Unicode and is represented by character type, while binary data is represented by byte type. Python 3 will not mix byte type and character type in any implicit way. Therefore, string and byte packet cannot be spliced in Python 3 (it can be converted automatically in Python 2), You cannot search for strings in byte packets, nor can you pass strings into functions whose parameters are byte packets.
Bytes object is a sequence composed of a single byte as a basic element (8 bits, value range 0-255), which is an immutable object. The bytes object is only responsible for recording the object to be recorded in the form of binary byte sequence. What the object represents (such as what character it is) is determined by the corresponding encoding format decoding. In Python 3, bytes is usually used for network data transmission, binary image and file storage, etc. You can generate byte instances by calling bytes(), whose value form is b 'xxxxx', where 'xxxxx' is a sequence composed of one or more escaped hexadecimal strings (the form of a single X is: \ x12, where \ x is a lowercase hexadecimal escape character and 12 is a two digit hexadecimal number), Each hexadecimal number represents a byte (eight bit binary number, value range 0-255). For the same string, if different encoding methods are used to generate bytes object, different values will be formed
1 create byte
#Create byte b1 = b'hello' b2 = b"ello" b3 = b'''hello''' b4 = bytes('chinese','utf-8')
2. Set the encoding format of python file
# -*- coding: utf-8 -*- import sys sys.setdefaultencoding('utf-8') #Modify the system default encoding format
3 conversion of string and byte
#String to byte s1 = "chinese" s2 = s1.encode('utf-8') #str.encode() print(type(s2)) #<class 'bytes'> print(s2) #b'\xe4\xb8\xad\xe6\x96\x87' #Byte to string s3 = s2.decode('utf-8') #bytes.decode() print(type(s3)) #<class 'str'> print(s3) #chinese
3, Tuple
Tuples are similar to lists, but tuples belong to immutable sequences, so tuples:
- The value of an element cannot be modified
- Tuples are represented by ()
1.1 creating tuples
t1 = () #Create an empty tuple #perhaps t1 = tuple() #Empty tuple t2 = (1,) #To create a tuple with one element, the comma after it is required. Otherwise, it is impossible to distinguish whether it is a () expression or a tuple Or: t2 = 1, t3 = (1,4,True,'hello') t4 = 10,20,30 #t4 = (10,20,30) t5 = tuple('abc')
1.2 member visits
t1 = (10,7,12,23) print(t1[0]) #print(t1[4]) subscript out of bounds IndexError t2 = (1,2,[4,5]) t2[2][0] = 10 #The element of tuple cannot be modified, but if the element is a variable list, the list element can be modified
1.3 general operation
#1. Connection t1 = (1,2,3) t2 = (4,5,6) t3 = t1 + t2 print(t3) #(1,2,3,4,5,6) #2. Repeat print(t1 * 2) #(1,2,3,1,2,3) #3. Slice print(t3[0:3]) #(1,2,3) print(t3[3:]) #(4,5,6) print(t3[-1::-1]) (6,5,4,3,2,1) #4. Member operator print(3 in t3) #True print(2 not in t3) #False #5. Number of elements print(len(t3)) #6. Maximum value print(max(t3)) #6 print(min(t3)) #1
1.4 other operations
-
Conversion of tuples and lists
t1 = (10,20,30) l1 = [1,2,3] #List tuple print(tuple(l1)) #Tuple to list print(list(t1))
-
lookup
t1 = (10,20,30,10) print(t1.index(20)) #Find the first element with a value equal to 20 print(t1.count(10)) #Returns the number of 10 in a tuple
-
ergodic
t1 = (10,20,30,10) for value in t1: print(value) #Get both subscript and value for index,value in enumerate(t1): print(index, value) #Traversal by subscript for i in range(len(t1)): print(t1[i])
1.5 two dimensional tuple
t1 = ((1,2,3),(4,5,6)) #ergodic for elem in t1: for value in elem: print(value, end = ' ') print('')# To wrap.
1.6 unpacking of sequence - "*"
#Tuple unpacking #The number of variables is consistent with the number of elements t1 = (11,20) a, b = t1 print(a,b) #11 20 a, b = 2, 3 a,b,c = (1,2,3) #The number of variables is different from the number of elements #a=10,c=50,_ Get (20,30) a, *_, c = 10,20,30,50 print(a,c,_) #10 50 [20, 30] #a=10, b=20,c obtains the remaining elements a, b, *c = 10,20,30,50 print(a,b,c) #10 20 [30, 50] #*Unpack print(*(1,2,3)) # 1 2 3 #range unpacking a, b, c = range(3) #a=0,b=1,c=2 #List unpacking a,*_b,c = [1,2,3,4,5] print(a,_b,c) # 1 [2, 3, 4] 5 #String unpacking a,b,c = '123' print(a,b,c) # a='1',b='2',c='3'
4, Dictionary
The dictionary belongs to an unordered sequence. The elements are stored unordered. Data access is carried out through hash. The dictionary is a variable container. The dictionary stores key value pairs: key: value one by one. We use the key to get the corresponding value, which is very similar to our Chinese dictionary.
- The key in the dictionary must be unique and must be an immutable type
- Dictionary search speed is relatively fast
2.1 dictionary creation
d1 = {} #Empty dictionary d1 = dict() #Empty dictionary d2 = {'name':'Spicy lobster','taste':'delicious'} d3 = dict(a=1,b=2) d4 = dict([('a', 1),('b', 2)]) d5 = dict({'a':1,'b':2})
2.2 dictionary operation
#Store scores of multiple students dict1 = {"jack":78,"hanmeimei":99,"lilei":60} print(dict1) #1. Element access #Get syntax: dictionary name [key] print(dict1["hanmeimei"]) #print(dict1["tom"]) #KeyError: 'tom' #Dictionary name get() result = dict1.get("lilei",'1') # If there is no lilei key, the default value of 1 will be returned and no error will be reported print(result) #2. Add: when the specified key does not exist, it means to add dict1["tom"] = 70 print(dict1) #However, if the key already exists, it means that the value is modified dict1["jack"] = 50 print(dict1) #3. Delete pop and return the value corresponding to the specified key #Note: the entire key value pair can be deleted directly by pressing the key dict1.pop("jack") print(dict1) del dict1['lilei'] #Delete key value pairs without returning values dict1.clear() #Empty dictionary del dict1 #Delete entire dictionary #4 dictionary merging a = {1:'hello'} b = {2:'world'} a.update(b) print(a) #5. Traversal dict2 = {"jack":78,"hanmeimei":99,"lilei":60} #Method 1: get only keys for key in dict2: value = dict2[key] print(key,value) #Method 2: only get values #values, the result is a list, which is treated as a list print(dict2.values()) for value in dict2.values(): print(value) #Three key method: get value at the same time #items, the result is a list, and the elements in the list are tuples print(dict2.items()) #[('jack', 78), ('hanmeimei', 99), ('lilei', 60)] for key,value in dict2.items(): print(key,value) #Mode 4 for index,key in enumerate(dict2): value = dict2[key] print(index,key,value) #6. Get the number of key value pairs print(len(dict1)) #7. Member operation d2 = {'name':'Spicy lobster','taste':'delicious'} print('name' in d2) #Determine whether a key is in the list