Python learning Notes (String)

Source: Internet
Author: User

a variables

1 Creating a variable: d=4 means assigning an integer object ' 4 ' to the variable D

Remember: All data is an object

Remember: All variables are a reference to a data object

Analysis: The reference count inside Python. Sys.getrefcount

2 Variable Naming conventions:

(1) Use only letters and underscores

(2) cannot use keywords such as if and so on

(3) Case sensitive

3 Assignment:

Remembering Polymorphic Properties

Multiple assignments

Delete: Del

An equal sign = is an assignment, and two equals = = is a comparison

Note: The three built-in must be cooked. Type,help,dir

4 Common basic data types:

(1) Int integral type

(2) Boolean Boolean value

(3) String strings

(4) List

(5) Tuple tuples

(6) Dict dictionary

5 variable and non-volatile data types

(1) Immutable type: int, string, tuple (by ID () unique identifier)

(2) Variable type: list, dict

two string : Help (str)

1 accessing strings, sequences

A= ' Wujiadong '

Print (A[0])

Print (Len (a))

Print (A[8])

Print (a[:])

2 Replacement string

B=a.replace (' Wujiadong ', ' Jiangrui ') #前面是老字符串, followed by a new string, note to enclose

Print (b)

3 string concatenation

Method One: "Pass the + number"

A= ' Wujiadong '

B= ' Jiangrui

C= ' a ' + ' B ' # + number stitching

Print (A+B)

Method Two: "Through placeholder%"

A= ' My name is Wujiadong. '

Print (a)

B= ' My name is%s wujiadong '% ' Xiaowu ' #

Print (b) Result: My name is Xiaowu Wujiadong

%s-placeholders for strings

%d-placeholders for numbers

Method Three: The best way to ". Join ()"

A= ' Wu '

b= ' Jia '

C= ' Dong '

D1= '. Join ([a,b,c])

D2= ' * '. Join ([a,b,c])

Print (D1) Result: Wujiadong

Print (D2) Result: Wu*jia*dong

4 Read and write text

W-write R-read A-append

A=open (' A.txt ', ' W ') #创建打开文件

A.write (' Hi.\nsecond hi. ') #写内容只能写字符串

A.close () #关闭文件

A=open (' A.txt ', ' R ') #这里写完后可以help (d) See which methods are read

Print (list (a)) Result: [' hi.\n ', ' second hi. '] The #用list () conversion function returns the actual contents of the file (in the form of a list)

#这里如果直接打印a会报错. A is a file object

Print (A.read (100)) Result: The result can be directly output, 100 can not write. If re-read, need A.seek (0)

#两次输出

Print (A.readline ()) Result: output Hi and blank line

Print (A.readline ()) Result: Output second hi

>>> a=open (' 1.txt ', ' R ')

>>> a=open (' 1.txt ', ' W ')

>>> a.write (' wowowo\naiaiaiai\nyouyouyou ')

25

>>> A.close ()

>>> a=open (' 1.txt ', ' R ')

>>> Import Linecache

Via Module Linecache

Print (Linecache.getline (' 1.txt ', 1))

Or

>>> lines=linecache.getlines (' 1.txt ')

>>> Print (lines)

5 Modifying strings

A= ' This is Jiadong '

A=a.replace (' This ', ' that ') #替换

Print (a) result: that's Jiadong

6 string formatting

(1) through the format () function:

Print (' {0} love {1} {2} '. Format (' I ', ' Jia ', ' dong ') result I love Jia Dong

Print (' {} {} '. Format (' Jia ', ' dong ') result Jiadong

Print (' {1}{0}{1} '. Format (' Jia ', ' dong ') result Dongjiadong

(2) by keyword:

Print (' {A} love {b} {c} '. Format (a= ' i ', b= ' Jia ', c= ' dong ')) result I love Jia Dong

Print (' {0} love {a} {b} '. Format (' Jia ', a= ' Wu ', b= ' dong ') result Jia Love Wu Dong #综合使用, positional parameters must be before the keyword parameter

(3) by subscript:

p=[' Jiadong ', 18]

Print (' {0},{0} '. Format (p)) results [' Jiadong ', 18],[' Jiadong ',] #两0之间可以有逗号, or no comma

Print (' {0[0]},{0[1]} '. Format (p)) Results jiadong,18

7 Format qualifier: (Syntax is {} with: #)

A Fill and align:

(1) Padding is used in conjunction with alignment

(2) ^, <, > is centered, left-aligned, right-aligned, followed by width

(3): The character after the number is filled, can only be one character, not specified by default with a space padding

Print (' {: >8} '. Format (' 189 ')) Results 189 #大于号右边靠

Print (' {: <8} '. Format (' 189 ')) Results 189 #小于号左边靠

Print (' {: 0<8} '. Format (' 189 ')) results 18900000 #用0填充

Print (' {: a>8} '. Format (' 189 ')) results aaaaa189 #用a填充

B precision and type F

Print (' {:. 2f} '. Format (321.33345)) Results 321.33 # where. 2 represents a precision of 2 length, and F indicates the folder type

C Other types

(1) mainly in the system, B, D, O, X are binary, decimal, octal, hexadecimal.

Print (' {: b} '. Format (17)) Result 10001

Print (' {:d} '. Format (17)) result 17

Print (' {: o} '. Format (17)) Result 21

Print (' {: x} '. Format (17)) Result 11

(2) The thousand separators that can be used to make the amount

Print (' {:,} '. Format (1234567890)) result 1,234,567,890

D string Formatting symbolic meaning

(1)%c formatted string machine ASCII code

Print ('%c '% 97) result A

Print ('%c%c%c '% (97,98,99)) results a B C

(2)%s formatted string

Print ('%s '% ' I love wujiadong ') result I love Wujiadong

(3)%d formatted integer

Print ('%d+%d=%d '% (4,5,4+5)) Results 4+5=9

(4)%o formatting unsigned octal numbers

Print ('%o '% 10) Results 12

(5)%x format unsigned hexadecimal number

Print ('%x '% 10) result A

(6)%x formatted unsigned hexadecimal number (uppercase)

Print ('%x '% 10) result A

Print ('%e '%27.123) results A0

(7)%f formatted fixed-point number, you can specify the precision after the decimal point

Print ('%f '%27.123) results 27.123000

(8)%e of fixed-point number by scientific method

Print ('%e '%27.123) results 2.712300e+01

(9)%e with%e

Print ('%E '%27.123) results 2.712300E+01

(%g) Depending on the size of the value, use%f or%e

Print ('%g '%27.123) results 27.123

(one)%g effect with%g

Print ('%G '%27.123) results 27.123

E string formatting operator auxiliary instruction

Python learning Notes (String)

Related Article

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.