Python Basic Two _ operation string common method, dictionary, file read

Source: Internet
Author: User

A. Common methods of strings:

Name.captitalize () #字符串首字母大写

Name.center ("*") #字符串居中

Name.count (' Huan ') #查询次数

Name.endswith ('. xls ') #判断字符串是否以XX结尾

Name.start with (' 138 ') #判断字符串是否以XX开头

Name.uper () #都变大写

Name.lower () #都变小写

Name.find (' P ') #查找元素p, element subscript not found, no error, return-1

Name.index (' P ') #找不到元素会报错

NAME[1] #字符串也可根据下标来取值

Name.isdigit () #判断是否为纯数字

Name.isidentifier () #判断是否为合法的变量名

Name.strip () #去掉字符串两边的内容, whitespace and line breaks are removed by default

Name.lstrip () #去掉左边的

Name.rstrip () #去掉右边的

name.replace () #替换字符串, replace the front with the back

Name.zfill () #补0

name.split () #分割字符串 (turns the string into a list. By default, separated by spaces and newline characters), what to divide

name.join () #是把list变为字符串, connect to a string eg: "Lxy, ZYF, Wdz" Print (', '. J Oin (stus))----->lxy, ZYF, Wdz

Import string

String.ascii_letters #所有的大, lowercase letters

String.ascii_lowercase #所有的小写字符

String.ascii_uppercase #所有的大写字符

String.digits #所有的数字

String.punctuation #所有的特殊字符

Second, array

Array loops

stus=[' xyz ', ' 2 ', ' 3 ', ' 4 ']

For Stu in Stus:

Print (STU)

Slices: A way to take a list of values

Stus[1:3] #顾头不顾尾 Stus[:3] If the beginning of the element is not written, it is from the front of the beginning

STUS[-1] #代表最后一个元素

Stus[1:] #结束元素不写的话, representing the most to the last one

stus[:] #取整个list

Stus[0:11:2] #第二个: After the value of the step, the number of elements to take the step is negative, subscript is also negative

Stus[:-1] #反转显示, slices also apply to strings

Three. Tuple list, but it is immutable

Once the tuple is defined, it cannot be changed.

The string is also immutable, cannot be modified, and needs to be re-assigned if you want to change it.

s= ' ABCEFGABC '

Print (S.replace (' abc ', '))

Print (s)--------->ABCEFGABC

To change the value of s, you need to re-assign the value

S=s.replace (' abc ', ')

Print (s)---------->EFG

a=[1,1,2,3,4,5,6,7]

For I in A:

If i%2!=0:

A.remove (i)

Print (a)---------->1,2,4,6

Why do you have 1?

First cycle: a=[1,2,3,4,5,6,7]

The second loop: a[1] has a value of 2 and skips 1, so print has 1

Defining variables: a,b,c=1,2,3

#定义a, b,c three variables, respectively

a,b=1,2

B,A=A,B------> Exchange value of A, a, b

B,a=a,b is not equivalent to B=a a=b

Non-NULL is true, not 0 is true, None

Not means to take an inverse

Four. Dictionary K-value dictionary is unordered de

shenyang={' name ': ' Shenyang ', ' age ': 18}

Increase:

shenyang[' friend ']= ' butty '

Shenyang.setdefault (' Home ', ' Sanhuan ')

#setdefault这种方式, if key already exists, it will not be modified

Modify:

shenyang[' friend ']= ' ugly '

Delete:

shenyang.pop[' friend '] #指定key删除

Del shenyang[' age '] #用dels删, key does not exist, will error

Value:

shenyang[' abc ']

Shenyang.get (' key ')

Shenyang.clear () #清空字典

Shenyang.popitem () #随机删除一个key

Shenyang.update (Yaoyuan) #把后面的字典加到前面的字典中

Shenyang.valus () #全部value

Shenyang.keys () #全部key

For k,v in Shenyang.item ():

Print (K,V) #通过. Item () mode to loop get K,v

Efficient Cycle Dictionary mode:

D={' A ': 1, ' B ': 2}

Print (D.item ())

---->dict.items ([' B ', 2), (' A ', 1)])

For K in D:

Print (K,d,get (k))

Five. file reading and writing

1. Open File

2. read/or write to him
3. Close the file

Open () file ()
1. Have you emptied the previous content?
2, can read the thing
3, the file does not exist when the error will be

f = open (R ' Haha.txt ', ' A + ', encoding= ' utf-8 ')
Print (' read ', F.read ()) #获取文件里面的所有内容
Print (' ReadLine ', F.readline ()) #读取文件一行的数据
Print (' Readllines ', F.readlines ()) #获取文件里面所有的数据, each row of data is placed in a list
F.write (' 5dianban ')


names = [' lxy\n ', ' zyf\n ', ' zxd\n ', ' lcs\n ']
For name in Names:
F.write (name+ ' \ n ')
F.writelines (names) #传一个list, and then writes each element in the list to a file
F.seek (0) #移动文件指针到最前面
Print (' read ', F.read ())
F.close ()

Read-only mode R read-write modes r+
Read only, cannot write, file does not exist will error #打开不存在的时候也会报错

Write mode W write read mode
Overwrite the previous file, inside the content, can not read, the file does not exist, will help you create a
As long as the R, the file does not exist will definitely error
As long as the W, the file content will definitely be emptied

Append mode A +
Can read, write, and not empty the previous content, the file does not exist will create

Python Basic Two _ operation string common method, dictionary, file read

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.