Today I'll summarize the number and string types in data types.
Preview:
# Write code,There are the following variables,Please implement each function as required (6 points, 0 for each sub-item).5 Points) name = " aleX" # 1) remove name Spaces on either side of the corresponding value of a variable,And output the processing results. # 2) judge name Does the value of a variable correspond to "al" Start,And output the results # 3) judge name Does the value of a variable correspond to "X" Ending,And output the results # 4) take name In the corresponding value of a variable“ l" Replace with“ p",And output the results # 5) take name The corresponding value of the variable is based on“ l" Division,And output the results. # 6) take name Variables correspond to capitalized values,And output the results # 7) take name Variables correspond to lower case values,And output the results # 8) Output please name The second character of the value corresponding to the variable? # 9) Output please name The first three characters of a variable's corresponding value? # 10) Output please name The last two characters of the corresponding value of a variable? # 11) Output please name Among the values corresponding to variables“ e" Location of index? # 12) Acquisition of subsequences,Remove the last character. as: oldboy Acquisition oldbo.
I. Digital (int, float)
In Python 3, the number types are only shaping, floating-point and complex. The complex number is hardly used in ordinary programming, so we only need to master shaping and floating-point type. (long shaping cancels the unified shaping in Python 3, so the shaping length is unlimited in Python 3)
Integral int: grade, age, grade, ID number, qq number, mobile phone number, etc.
ps:
level=10
Floating-point float: height, weight, salary, temperature, price
ps:
height=1.81
salary=3.3
1 n = 12 2 f = 1.5 3 print(type(n)) # int That's plastic surgery. 4 print(type(f)) #float That's floating point type. 5 6 #Scientific Counting Method python Application: (10 uses) e Substitution) 7 print(1.3e-3) #1.3e-3 --> 0.0013 8 print(1.3e3) #1.3e3 --> 1300.0
Characteristics of digital types:
1. Only one value can be stored
2. Once defined, unchangeable
3. Direct access
2. String type
Included in quotation marks (single, double, three), it consists of a string of characters
Uses (descriptive data): name, gender, address, educational background, password, etc.
ps:
1 s1 = 'lln' 2 s2 = "lln" 3 s3 = '''lln''' 4 s4 = """lln""" 5 6 print(type(s1)) 7 print(type(s2)) 8 print(type(s3)) 9 print(type(s4)) 10 #All are str String type
Value:
First of all, it should be clear that the whole string is a value, but the special point is that there is no character type in python. The string is composed of a string of characters. If you want to extract the characters in the string, you can also get them by subscript.
ps:
name = "lln"
name: Gets the value (lln) that is the whole string.
name[1]: Gets the character (l) that is the second position.
String splicing:
msg1='hello'
msg2=' world'
The use of + in a string:
msg1 + msg2
'hello world'
res=msg1 + msg2
print(res)
hello world
The use of * in strings:
msg1*3
'hellohellohello'
Note: Strings can no longer use other symbols.
Common String Method Full Solution:
#Remove spaces or characters on both sides name = " llN***" print(name) print(name.strip("*")) #Judging what ends and begins name = "lln" print(name.endswith("l")) #false print(name.startswith("l")) #true #replace name = "lln" print(name.replace("n","N")) #llN print(name.replace("l","L")) #LLn print(name.replace("l","L",1)) #Lln 1 Specified number of substitutions #format string str = "name:{},age:{},sex{}" print(str) #name:{},age:{},sex{} print(str.format("lln",22,"man")) #name:lln,age:22,sex:man str = "name:{0},age:{1},sex{0}" print(str.format("lln",22)) #name:lln,age:22,sex:lln str = "name:{x},age:{y},sex{z}" print(str.format(y=22,x="lln",z="man")) #name:lln,age:22,sex:man #Indexes name = "lln love" print(name.find("o")) #Return location print(name.find("a")) #Search cannot return-1 #Indexing print(name.index("o")) #Indexes o position print(name[4]) print(name[name.index("o")]) #Character statistics print(name.count("l")) #Letter l Number of occurrences print(name.count("l",0,4)) #Statistics 0-3 Median l Number of occurrences #segmentation name = "hello world" print(name.split()) #['hello', 'world']Separated by space by default name = "he:llo wor:ld" print(name.split(":")) #['he', 'llo wor', 'ld']Separators can be specified print(name.split(":", 1)) #There are two parts. # join Connect string arrays. name = " " print(name.join(["lln", "say", "hello", "world"])) # Iterable objects must all be strings #center,ljust,rjust,rfill Its format name = "lln" print(name.center(30,"-")) print(name.ljust(30,"*")) print(name.rjust(30,"*")) print(name.zfill(50)) #Fill in with 0 #expandtabs Replace tabs“\t' Number of characters of name = "lln\thello" print(name) print(name.expandtabs(1)) #small-caps(A lowercase letter) name = "lln" print(name.upper()) name = "LLN" print(name.lower()) #To determine whether capitalization (lowercase) is necessary, all capitals or lowercases must be capitalized. name = "lln" print(name.isupper()) print(name.islower()) # captalize,swepcase,title name = "lln" print(name.capitalize()) #title case name = "LlN" print(name.swapcase()) # Case reversal msg = "lln say hi" print(msg.title()) # Capital letters for each word # Judging capitalization of initial letters name = "Lln" print(name.istitle()) #Value and slice name = "hello world" print(name[0]) #h print(name[6]) #w #print(name[20]) Report errors print(name[-1]) #d print(name[-3]) #r print(name[1:3]) #el Section print(name[1:5]) #ello Section print(name[1:5:3]) #eo Section 3 is step size #Determine whether a string can be converted to a number # age=input('age: ') # if age.isdigit(): #If user input 12 returns true If abc Then return false # new_age=int(age) # print(new_age,type(new_age)) #Is the judgement blank?(Must be all blanks) name = "" print(name.isspace()) name = " " print(name.isspace()) print("===>") name = "egon123" print(name.isalnum()) # Strings consist of letters and numbers print(name.isalpha()) # Strings consist of only letters
Preview Answer:
# Write code,There are the following variables,Please implement each function as required name = " aleX" # 1) remove name Spaces on either side of the corresponding value of a variable,And output the processing results. print(name.strip()) # 2) judge name Does the value of a variable correspond to "al" Start,And output the results print(name.startswith("al")) # 3) judge name Does the value of a variable correspond to "X" Ending,And output the results print(name.endswith("X")) # 4) take name In the corresponding value of a variable“ l" Replace with“ p",And output the results print(name.replace("l","p")) # 5) take name The corresponding value of the variable is based on“ l" Division,And output the results. print(name.split("l")) # 6) take name Variables correspond to capitalized values,And output the results print(name.upper()) # 7) take name Variables correspond to lower case values,And output the results print(name.lower()) # 8) Output please name The second character of the value corresponding to the variable? print(name[1]) # 9) Output please name The first three characters of a variable's corresponding value? print(name[0:3]) # 10) Output please name The last two characters of the corresponding value of a variable? print(name[-2:]) # 11) Output please name Among the values corresponding to variables“ e" Location of index? print(name.index("e")) # 12) Acquisition of subsequences,Remove the last character. as: oldboy Acquisition oldbo. print(name[0:-1])