I. Existing string s = "I,am,lilei", please remove the "AM" character between two ways.
S[2:4]
S[s.find ("AM"): S.find ("AM") +len ("AM")]
Two. In Python, how do I modify a string?
A = "abc"
b = A.replace ("B", "a")
A = b
Three. What is the result of bool ("2012" = = 2012).
Flase
Four. A file test.txt is known to read as follows:
____________
It's 2012来.
2012 is not the end of the world.
2012 More fun.
_____________
1. Please output its contents.
2. Please calculate the original length of the text.
3. Remove the line breaks for this text.
4. Replace the character "2012" with "2013".
5. Take out the middle of the substring with a length of 5.
6. Please remove the last 2 characters.
7. From the beginning of the string, truncate the string so that it has a length of 11.
8. Save the string in {4} as test1.py text.
A = open ("Test.txt", "R")
Content = A.read ()
Dcontent = Content.decode (' Utf-8 ')
Print Len (dcontent)
Rn_dcontent = Dcontent.replace ("\ n", "" ")
Print Rn_dcontent
D = Rn_dcontent.replace ("2012", "2013")
Print D
E = D[d.find ("$", 5):d. Find ("2013", 5) +5]
Print E
Print D[-2:len (d)]
Print D[-2:]
Print "". Join ([d[-2],d[-1]])
Print D[0:11]
Rinfo = Content.replace ("2012", "2013")
f = open ("Test1.txt", "W")
F.write (Rinfo)
F.close ()
Five. Please describe the Python reference mechanism in the form of code.
Import Sys
A = 1
Print ID (a)
Print Sys.getrefcount (1)
A = 2
Print ID (a)
Print Sys.getrefcount (2)
b = 1
Print ID (b)
Print Sys.getrefcount (1)
Six. The following code is known
________
A = "Chinese programming"
b = A
c = A
A = "Python programming"
b = U '%s '%a
D = "Chinese Programming"
E = a
c = b
B2 = A.replace ("Medium", "Medium")
________
1. Please give reference count of "Chinese programming" for STR object
2. Please give a reference count of the STR object "Python Programming"
Import Sys
A = "Chinese programming"
b = A
Print Sys.getrefcount ("Chinese Programming")
c = A
Print Sys.getrefcount ("Chinese Programming")
A = "Python programming"
Print Sys.getrefcount ("Chinese Programming")
b = U '%s '%a.decode ("Utf-8")
Print Sys.getrefcount ("Chinese Programming")
D = "Chinese Programming"
Print Sys.getrefcount ("Chinese Programming")
E = a
Print Sys.getrefcount ("Chinese Programming")
c = b
Print Sys.getrefcount ("Chinese Programming")
B2 = A.replace ("Medium", "Medium")
Print Sys.getrefcount ("Chinese Programming")
Seven. The following variables are known
________
A = "string concatenation 1"
b = "string concatenation 2"
________
1. Please use more than four ways to stitch A and B into string C. and points out the merits and demerits of each method.
2. Please stitch A and B into string C, separated by commas.
3. Calculate the length of the newly stitched string and take out the seventh character.
A = "string concatenation 1"
b = "string concatenation 2"
Print A+b
Print "%s%s"% (A, B)
Print "{} {}". Format (A, B)
Print "". Join ([A, b])
Print ",". Join ([A, b])
c = "". Join ([A, b])
Print Len (C.decode ("Utf-8"))
Print C.decode (' utf-8 ') [6].encode (' Utf-8 ')
Eight. Read the string module and output the answers to the following questions based on the built-in method of the string module.
1. Contains 0-9 of the numbers.
2. All lowercase letters.
3. All punctuation marks.
4. All uppercase and lowercase letters.
5. Use the method you think is best to stitch the strings in {1}-{4} points into a single string.
Nine. Known string
________
A = "I,am,a,boy,in,china"
________
1. Suppose that boy and China are subject to change at any time, the example boy may be changed to girl or gay, and China may change to another country, how would you change the string above to be configurable.
2. Please use 2 ways to remove the characters "boy" and "China" in the meantime.
3. Please find out where the first "I" appears.
4. Find the position of the "I" character in "China" in string A.
5. Please calculate the string with a total of several commas.
A1 = "Boy"
a2 = "China"
A = "i,am,a,{name},in,{nation}". Format (name = a1, Nation = A2)
b = "i,am,a,% (name) s,in,% (Nation) S"%{' name ': A1, ' Nation ': A2}
Print a
Print B
A = ' I,am,a,boy,in,china '
Print A[a.find ("Boy"): A.find (' Boy ') +len ("Boy")]
Print A[a.find ("China"): A.find (' China ') +len ("China")]
Print A.split (",") [3]
Print A.split (",") [5]
Print A.find ("I")
Print A.index ("I")
Print A.find ("I", A.find ("China"))
Print A.rfind ("I")
Print A.count (",")
10. Save the Help document for the module string as a file.
Third lesson Python basic data type operations class