Python string object of study entry

I.
1. Concept string

	在python中被'',"",''' '''包含的便是字符串

( '' '' '' Will be displayed CRLF)
2. definition string:
String is actually a "orderly" character sequences
of characters -> String smallest unit - element> string of
characters strings are immutable
four definitions manner:

(1)a = "hello" (2) a ='hello' (3) a = """hello""" (4) a = str()

3. Common string

       1.capitalize:将字符串的首字母大写
       2.center:居中:一个参数定义的是填充的数量
 两个参数时,第二个参数定义的是填充的符号
       3.count:统计字符或字符串出现的次数
       4.encode:可以将字符串转化为字节(字节数据不可见,但python为了方便使用b''显示出来了)
        (建议进行编码转统一使用Unicode编码中的utf-8,注意:编码和解码一定要使用同一个标准),decode()方法是解码(只是字节的方法,不是字符串的方法)
       5.endswith:判断字符串是否以xxx结尾
       6.startswith:判断字符串是否以xxx开头
       7.find:查询字符或字符串第一次出现的位置,如果字符或字符串不存在,则返回-1
       8.index:查询字符或字符串第一次出现的位置,如果字符或字符串不存在,则抛出异常
       9.format:新的一种格式化字符串的方式,python3推出。print("{},{},{}".format(a,b,(a-b)))
	   10.istitle:判断是不是标题(标题的每个首字母是否是大写)
  	    isspace: 判断是不是空白字符
        islower:判断是不是小写字母
        isupper:判断是不是大写字母
        isalum :判断是不是由数字和字母组成
        isalpha:判断是不是又字母组成
        isdigit:判断是不是数字
       11.join:按照一定的规则拼接字符串,注意,参数是一个可迭代对象
       12.lower,upper:转小写,转大写
       13.title:转化字符串为一个符合标题规则的字符串
       14.split:用来切割,分隔字符串的(按照参数,分隔)
       15.rfind:找字符或字符串最后一次出现的位置
       16.strip:清除字符串两边的空格
       17.rstrip:只清除右边的空格
       18.replace:替换,第二个参数替换第一个参数

II. Slices
1.python itself does not provide a method has been truncated, all occurrences of the slices.
2. The slice is a python as we provide for cutting, dividing, in the interception of the container way.
(You can not use the set (sets are unordered), the dictionary can not cut)
sections only change in the return value, without changing the container itself.

容器[start :]  #从start位置开始截取容器,截取到末尾

容器[start :end] #从start位置开始,到end位置结束,不包含end位

容器[:end] #冒号左侧不写,默认就从下标为0的开始 

容器[start:end:step] #step表示步长, 默认是1

(When the step is negative, then cut from right to left, but the index is always from left to right)

Below you can see a face questions enhance memory:

给定一个列表ls[1,2,3,4,5,5,67]
1. 请翻转列表,请不要使用列表自身提供的方法 	 

A: ls [:: - 1]

2.ls[20:40],结果是什么?

Result: not being given, returns an empty list

Published 17 original articles · won praise 2 · Views 370

Guess you like

Origin blog.csdn.net/qq_44487069/article/details/104448656