Lesson Three: Python basic data Type lectures (2/3)

Source: Internet
Author: User


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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.