1. Basic data types
(1) Number-int
(2) string-str
(3) Boolean value-bool
2. Important methods in the INT type
(1) INT
To convert a string to a numeric type:
# Converts a string of byte numbers to an int type
# Converts a string of byte numbers to int type a = ' 123 ' b = Int (a) print (type (a), a) print (type (b), b) # converts num to decimal num = ' 0011 ' v = int (num, in hexadecimal notation) base=16) Print (v)
3. Main methods of String
Examples are detailed:
(1) Capitalize ()
Capitalize first letter
Test = ' hkey ' V = test.capitalize () print (v) # execution Result: hkey
(2) lower () Casefold ()
Lowercase the string uppercase, Casefold () to lowercase some letters from other countries
Test = ' HkEy ' v1 = test.casefold () v2 = Test.lower () print (v1, v2) # Execution Result: HkEy HkEy
(3) Center ()
Set the width and center the content, 20-generation refers to the total length; * Substituting blank padding
name = ' hkey ' v3 = Name.center (' # ') print (v3) # Execution Result: ####### #hkey ########
(4) Count ()
Find the number of subsequence occurrences in a string
name = ' Hkeyxiaoxiao ' v = name.count (' x ') print (v) # execution Result: * can set start position and end position name = ' Hkeyxiaoxiao ' v1 = name.count (' x ', 0, 8) print (v1) # Direct result: 1
(5) StartsWith () EndsWith ()
StartsWith (): What sequence begins with the result of a Boolean value
EndsWith (): End with what sequence, the result is a Boolean value
name = ' hkey ' v = name.startswith (' h ') print (v) # execution Result: Truev1 = name.endswith (' y ') print (v1) # execution Result: True
(6) Find () RFind ()
Look back from the beginning, find the first one, get its index, the result is:-1 means not found
name = ' Hkeykey ' # starts looking for the first matching sequence and prints the starting index position of the sequence V1 = name.find (' key ') print (v1) # Execution Result: # (Sub, start=none, End=none) Start: Start position end: End Position v2 = name.find (' key ', 0, 3) print (v2) # execution Result: -1name = ' Khkeykey ' # find character index position from right to left print (name.rfind (' Y ')) # Row Result: # 7
(7) format () Format_map ()
Format () is formatted to replace the placeholder specified in a string with a value, the placeholder is formatted with {} format_map (), and the value is passed to the placeholder for the corresponding key in the form of a dictionary
# format, replace the placeholder specified in a string with the value test = ' I am {name} ', age {A} ' print (test) # execution Result: I am {name}, age {a}v = Test.format (name= ' hkey ', a=2 0) Print (v) # execution Result: I am hkey, Age # can use index directly to specify placeholder test = ' I am {0} ', age {1} ' print (test) # execution Result: I am {0}, age {1}v = Test.format (' hkey ') print (v) # execution Result: I am hkey, age 20# format_map Pass the value to the placeholder of the corresponding key in the form of a dictionary test = ' I am {name} ', age {A} ' V1 = Test.form At_map ({' name ': ' hkey ', ' a ': ') ' Print (v1) # execution Result: # I am hkey, age 20
(8) Index ()
Look back from the beginning, find the first one, get its index, and if not, error.
name = ' hkey ' v = name.index (' y ') print (v) # Execution Result: # 3v1 = Name.index (' z ') print (v1) # execution Result: # Traceback (most recent call last): # File "e:/learn_python/day11/s2.py", line 119, in <module># v1 = name.index (' z ') # valueerror:substring Not found
(9) Isalnum
Whether the string contains only letters and numbers
Test = ' abcd+_ ' V = test.isalnum () print (v) # Execution Result: # falsetest = ' abcd ' V = test.isalnum () print (v) # Execution Result: # True
(Ten) Expandtabs
If the string contains a tab ' \ T ', the string is split as a tab character.
s = ' Username\temail\tpassword\nhkey\[email protected]\thkeyy ' V = s.expandtabs () print (v) # Execution Result: # username Email password# hkey [Email protected] Hkeyy
(one) Isalpha ()
Determines whether a string contains a number, contains a number of False, and does not contain a number: True
s = ' Superman ' V = S.isalpha () print (v) # Execution Result: # True
(Isdecimal) IsDigit () IsNumeric ()
Determines whether a string is a number
IsDigit () digital notation for distinguishing special symbols
IsNumeric () Can judge the number of Chinese characters ' two '
Test = ' ② ' v1 = test.isdecimal () v2 = Test.isdigit () print (v1, v2) # Execution Result: # False Truetest1 = ' two ' V1 = test1.isdecimal () v2 = Te St1.isdigit () # Can judge the Chinese number's notation V3 = Test1.isnumeric () print (V1, v2, v3) # Execution Result: # false False True
(+) Islower ()
Determines the string lowercase.
test= ' hkey ' V=test.islower () print (v) #执行结果: #True
(+) isprintable ()
Determines whether a string contains non-visible characters, such as \ t \ n, and so on
Test = ' abcdefg\t ' V = test.isprintable () print (v) # Execution Result: # False
(isspace) ()
Determine if variables are all spaces
Test = ' V = test.isspace () print (v) # Execution Result: # True
(+) Istitle () title ()
Istitle () determines whether the first letter is a string of uppercase
Title () converts a string to a heading in uppercase letters
Test = ' My heart would go on ' V = test.istitle () v1 = Test.title () print (v) print (v1) # execution Result: # false# My heart'll go on
() Join ()
Stitching each element in a string according to the specified delimiter
Test = ' can't see your smile how can I sleep ' v = ' # '. Join (test) print (v) # execution Result: # see # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
(+) Ljust () Rjust ()
Set width:
Ljust () string drop left
Rjust () string put to the right
name = ' hkey ' v1 = name.ljust (*, ' * ') v2 = name.rjust (+, ' * ') print (v1) print (v2) # Execution Result: # hkey****************# *********** Hkey
(+) Zfill ()
Cannot specify character, just 0 padding to left
name = ' hkey ' v1 = name.zfill print (v1) # execution Result: # 0000000000000000hkey
(isupper) () Upper ()
Upper () converts a lowercase string to uppercase
Isupper () determines whether a string is uppercase
Test = ' My heart'll go on ' v1 = test.isupper () v2 = Test.upper () print (v1) print (v2) # Execution Result: # false# My heart'll go on
(+) Lstrip () Rstrip () strip ()
Lstrip () Remove string header special symbol and space
Rstrip () Remove the string trailing special symbols and spaces
Strip () Remove string end and end and space
name = ' \nhkey\n ' v1 = Name.lstrip () v2 = Name.rstrip () v3 = Name.strip () print (v1) print (v2) print (v3) # Execution Result: # v1:# hkey# # v2: # # hkey# v3:# hkey
(a) Maketrans ()
Translate () Maketrans () replaces two one by one corresponding strings
Translate () replaces two strings in Maketrans
test1 = ' ABCDEFG ' test2 = ' 1234567 ' v = ' adfasdfzcvdrfhkljwerto ' m = Str.maketrans (test1, test2) new_m = v.translate (m) print ( new_m) # Execution Result: # 1461s46z3v4r6hkljw5rto
(Partition () Rpartition () split () Rsplit ()
Partition () divides a string into three points and splits the delimiter as a separate element
Rpartition () splits the string into three points starting from the right and splits the delimiter as a separate element
Split () splits the string with the specified character, the split list does not contain the split characters, and the number of splits can be performed
Rsplit () starts from the right, splits the string with the specified character, does not contain the split character in the split list, and performs the split number
Test = ' ASDFADFSDFXZSCV ' # divides the string into three points and splits the delimiter as a separate element v = test.partition (' s ') print (v) # starts from the right, divides the string into three points, and splits the delimiter as a separate element v1 = test.rpartition (' s ') print (v1) # Splits the string with the specified character, does not contain the split character in the split list, and can perform the split number v2 = Test.split (' s ', 1) print ( V2) # starts from the right, splits the string with the specified character, does not contain the split character in the split list, executes the split number V3 = Test.rsplit (' s ', 1) print (V3) # Execution Result: # # v:# (' A ', ' s ', ' DFADFSDFXZSCV ') # v1:# (' Asdfadfsdfxz ', ' s ', ' CV ') # v2:# [' Asdfadfsdfxz ', ' CV ']# v3:# [' A ', ' DFADFSDFXZSCV ']# v4:# [' asdfadfsdfxz ', ' CV ']
(Splitlines) ()
Split, can only be based on: True, False whether to preserve line breaks
Test = ' adfaf\nadfadf\nadfaf\n ' v = test.splitlines (True) v1 = test.splitlines (False) print (v) print (v1) # execution Result: # v:# [' Adfaf\n ', ' adfadf\n ', ' adfaf\n ']# v1:# [' ADFAF ', ' adfadf ', ' ADFAF ']
(+) StartsWith () EndsWith ()
StartsWith: Starting with what
EndsWith: At what end
Test = ' hkey ' # with what start V1 = Test.startswith (' h ') # with what end v2 = Test.endswith (' e ') print (v1) print (v2) # Execution Result: # true# False
(+) Swapcase ()
Uppercase and lowercase conversions
name = ' HkEy ' V = name.swapcase () print (v) # Execution Result: # HkEy
(+) Isidentifier ()
Detects if a string starts with a letter
Test = ' 1a1dsf123 ' Print (Test.isidentifier ()) # execution result; # False
() replace ()
Replace string
name = ' Hkeykey ' # replace ' K ' in string with ' F ' to replace up to 1 times print (Name.replace (' K ', ' F ', 1)) # Execution Result: # Hfeykey
Summarize:
Several common properties in a string:
Join (), split (), find (), strip (), upper (), lower (), lower ()
4. Common string manipulation
(1) Get characters by index
name = ' hkey ' Print (name[2]) # execution Result: # E
(2) slicing
To slice a string by its starting value, ending value, and stride size
name = ' hkey ' v1 = Name[0:2]v2 = Name[0:4:2]print (v1) Print (v2) # Execution Result: # v1:# hk# v2:# He
(3) Gets the length of the string
name = ' hkey ' Print (len (name)) # Execution Result: # 4
5. Manipulating string parsing
Once created in memory, the string cannot be modified, and if the string is modified or spliced, a new string will inevitably be generated.
[Python] Basic data types and properties (previous)