The cognition and application of string
Cognition 1: The concept of a string
What is cognitive 2:acii Unicode UTF8?
Python default file encoding is ASCII
1. Len's Considerations (decode () need testing)
>>> len ("Analysis")
4
>>> Len (U "Analysis")
2
2. Escaping a string
Print (' adb\ ')
Print (' adbd\n ')
3. String followed by a small tail
A = u "hahaha"--Indicates Unicode encoding
B = r "asdfasd\n"--no escaping required
4. Access substring, sequence comes
Members are ordered in order and can be accessed to one or more of its members by an subscript offset
A = "123456789"
A[0]
A[len (a)-1]
A[-1]
A[0:]
A[0:1]
A[:-1]
5. Replacing strings
>>> a = "abc"
>>> A.replace ("A", "CCCCC")
' CCCCCBC '
>>> Print a
Abc
>>> ID (a)
34392688
>>> d = a.replace ("A", "CCCCC")
>>> Print D
Cccccbc
>>> ID (d)
38982336
6. String concatenation
"abc" "De"
"abc" + "de" = "ABCDE"
1) Super Ugly don't use
2) string template for optional scenarios
%s String placeholder
Placeholder for%d numbers
"My name is%s lilei"% "Hameimei ' s"
"My name is%s Lilei"% 1--contains invisible transitions
"My name is%d Lilei"% "1234"--error, no conversion
"My name is%s lilei%s"% ("Hameimei's", "Ten years old")--% followed by parentheses
3) Excellent splicing scheme
Understanding the Join method, the parameter of the join is a tuple
>>> a = "a"
>>> B = "B"
>>> c = "C"
>>> "". Join ([a,b,c])
' ABC '
>>> ",". Join ([a,b,c])
' A,b,c '
7. Read and write text came up
W-write
R-read
A-append
D=open (' A.txt ', ' W ')
D.write (' Hi. \ n Hello world \ Second hi \ n ')
D.close ()
Dr = Open (' A.txt ', ' R ')
Dr.readline ()
Dr.readline ()
Dr.tell ()--Displays the position of the cursor
Dr.seek (0)--set the cursor position at 0L
Dr.read (10)
Dr.readlines ()
Lesson Three: Python basic data Type lectures (2/3)