The second week of class notes 2th

--- --- restore content begins

1. 

 

 

 

 

 

2. The index takes a single value

Take multiple values ​​called slices,

Slice: taking a plurality of values

From left to right values:

Principles: care regardless of the end

1, a[0:3] abc

2, a[-5:-2] abc

3, a[0:-2] abc

From right to left values:

Principles: care regardless of the end

1, [2 :: - 1] cba

2, from [-3 :: - 1] cba

3, a[2:-6:-1] cba a[2:-5:-1] cb

 

Slice Law: care regardless tail

 

 

If achieved second place is the last one empty

 

 

If you can play from start to finish directly colon

 

 

If the negative take a slice, or a positive value

If the counter value is the value added back in step

 

 

3.

 

 

The default step is a lag between the two steps elements

 

When the slice counter value must be added step backward

 

 

4. A common method of number string

 

 

Realized capital: ( or direct printing )

 

 

 

Achieve lowercase:

 

 

To ...... beginning:

b=name.startswith(“ab”)

To ...... end:

c=name.endswith(“ab”)

 

 

Remove leading and trailing spaces, special characters:

Printa.strip())

example

a=”&&a&bc&&”

Print(a.strip(“&”))

Alternatively: After replacing the character number is added to the number of replaceable

 

 

 

 

String methods:

1>s = "abC"

upper,lower

print (s.upper ()) to achieve full string uppercase

print (s.lower ()) to achieve all lowercase string

2> the replace  replace

a = "abacad"

print(a.replace("a","中国"))

Print (a.replace ( "A" , "China", 2)) represents the number 2

3>capitalize,

The first letter capitalized, remaining letters lowercase

4> swapcase , # the case Flip

5>strip

Remove leading and trailing spaces, special characters

print(a.strip())

a = "&&a&bc&&"

print(a.strip("&"))

6>startswith,endswith

The result is bool value, support sections

print (s.startswith ( "a") ) is determined to. . . beginning

print (s.endswith ( "a") ) is determined to. . . end

Print (s.startswith ( "A" , l, 4)) is determined slice portion

Public methods:

7>count()

s = "abac"

# a print (s.count ( "a ")) the number of elements appearing

. 8> len () Print (len (S)) length of the object

9>split str --->list

str divided into a list, separated by spaces by default

 

 

10> s.split () default space

s.split ( " Symbol")

 

 

​​

11>join list ----> str

Connecting elements inside the list specified symbol

Form: "Separator" .join (list)

 

 

join to add something to the original characters

name.join(“*”)

12> find   position in the string of characters appearing

example

a = “231615”

print (a.find ( "1")       ) results 2

print (a.find ( "1",     3)) the result is 4

print (a.find ( "1",     4)) the result is -1

13> rsplit as delimiter

example

 a=6+9

print (a.rsplit ( "+")      ) results [ '6', '9']

 Delimiter to divide the content stored separately method

example

a=6+9

print(a.rsplit(”+”))

b , c = a.rsplit ( "+", 1)

print (int (a) + int         (b)) Results 15

 

 

 

--- end --- restore content

1. 

 

 

 

 

 

2. The index takes a single value

Take multiple values ​​called slices,

Slice: taking a plurality of values

From left to right values:

Principles: care regardless of the end

1, a[0:3] abc

2, a[-5:-2] abc

3, a[0:-2] abc

From right to left values:

Principles: care regardless of the end

1, [2 :: - 1] cba

2, from [-3 :: - 1] cba

3, a[2:-6:-1] cba a[2:-5:-1] cb

 

Slice Law: care regardless tail

 

 

If achieved second place is the last one empty

 

 

If you can play from start to finish directly colon

 

 

If the negative take a slice, or a positive value

If the counter value is the value added back in step

 

 

3.

 

 

The default step is a lag between the two steps elements

 

When the slice counter value must be added step backward

 

 

4. A common method of number string

 

 

Realized capital: ( or direct printing )

 

 

 

Achieve lowercase:

 

 

To ...... beginning:

b=name.startswith(“ab”)

To ...... end:

c=name.endswith(“ab”)

 

 

Remove leading and trailing spaces, special characters:

Printa.strip())

example

a=”&&a&bc&&”

Print(a.strip(“&”))

Alternatively: After replacing the character number is added to the number of replaceable

 

 

 

 

String methods:

1>s = "abC"

upper,lower

print(s.upper()) 实现字符串全部大写

print(s.lower()) 实现字符串全部小写

2>replace 替换

a = "abacad"

print(a.replace("a","中国"))

print(a.replace("a","中国",2)) 2 表示个数

3>capitalize,

首字母大写,其余字母小写

4>swapcase, #大小写翻转

5>strip

去掉开头和结尾的空格,特定的字符

print(a.strip())

a = "&&a&bc&&"

print(a.strip("&"))

6>startswith,endswith

结果是bool 值 ,支持切片

print(s.startswith("a")) 判断以。。。开头

print(s.endswith("a")) 判断以。。。结尾

print(s.startswith("a",1,4)) 切片部分判断

公共方法:

7>count()

s = "abac"

print(s.count("a") ) # a 元素出现的次数

8>len() print(len(s)) 对象的长度

9>split str --->list

str分割为列表,默认以空格分割

 

 

10>s.split() 默认空格

s.split("符号")

 

 

​​

11>join list ----> str

列表里面元素用指定符号进行连接

形式:”分隔符“.join(list)

 

 

join往原先的字符中添加一些东西

name.join(“*”)

12>find   字符串中字符出现的位置

例子

a = “231615”

print(a.find(“1”))       结果为2

print(a.find(“1”,3))     结果为4

print(a.find(“1”,4))     结果为-1

13>rsplit为分割符

例子

 a=6+9

print(a.rsplit(”+”))      结果为['6', '9']

 分割符中把分割的内容分开保存的方法

例子

a=6+9

print(a.rsplit(”+”))

b,c= a.rsplit(”+”,1)

print(int(a)+int(b))         结果为15

 

 

 

---恢复内容开始---

1. 

 

 

 

 

 

2.索引取单个值

取多个值叫切片,

切片:取多个值

从左到右取值:

原则:顾头不顾尾

1, a[0:3] abc

2, a[-5:-2] abc

3, a[0:-2] abc

从右到左取值:

原则:顾头不顾尾

1, a[2::-1] cba

2, a[-3::-1] cba

3, a[2:-6:-1] cba a[2:-5:-1] cb

 

切片规律:顾头不顾尾

 

 

若取得第二位值为最后一位用空

 

 

若从头到尾可以直接打冒号

 

 

若是为负数取切片,还是正向取值

若取值为反向取值在后边加步长

 

 

3.

 

 

默认步长为1,两个元素之间相隔几步

 

当切片反向取值时一定要加反向步长

 

 

4.字符数串的常用方法

 

 

实现大写:(或直接打印)

 

 

 

实现小写:

 

 

……开头:

b=name.startswith(“ab”)

……结尾:

c=name.endswith(“ab”)

 

 

去掉开头和结尾的空格,特定的字符:

Printa.strip())

例子

a=”&&a&bc&&”

Print(a.strip(“&”))

替换:在替换后的字符后加数字是替换的个数

 

 

 

 

字符串的方法:

1>s = "abC"

upper,lower

print(s.upper()) 实现字符串全部大写

print(s.lower()) 实现字符串全部小写

2>replace 替换

a = "abacad"

print(a.replace("a","中国"))

print(a.replace("a","中国",2)) 2 表示个数

3>capitalize,

首字母大写,其余字母小写

4>swapcase, #大小写翻转

5>strip

去掉开头和结尾的空格,特定的字符

print(a.strip())

a = "&&a&bc&&"

print(a.strip("&"))

6>startswith,endswith

结果是bool 值 ,支持切片

print(s.startswith("a")) 判断以。。。开头

print(s.endswith("a")) 判断以。。。结尾

print(s.startswith("a",1,4)) 切片部分判断

公共方法:

7>count()

s = "abac"

print(s.count("a") ) # a 元素出现的次数

8>len() print(len(s)) 对象的长度

9>split str --->list

str分割为列表,默认以空格分割

 

 

10>s.split() 默认空格

s.split("符号")

 

 

​​

11>join list ----> str

列表里面元素用指定符号进行连接

形式:”分隔符“.join(list)

 

 

join往原先的字符中添加一些东西

name.join(“*”)

12>find   字符串中字符出现的位置

例子

a = “231615”

print(a.find(“1”))       结果为2

print(a.find(“1”,3))     结果为4

print(a.find(“1”,4))     结果为-1

13>rsplit为分割符

例子

 a=6+9

print(a.rsplit(”+”))      结果为['6', '9']

 分割符中把分割的内容分开保存的方法

例子

a=6+9

print(a.rsplit(”+”))

b,c= a.rsplit(”+”,1)

print(int(a)+int(b))         结果为15

 

 

 

---恢复内容结束---

1. 

 

 

 

 

 

2.索引取单个值

取多个值叫切片,

切片:取多个值

从左到右取值:

原则:顾头不顾尾

1, a[0:3] abc

2, a[-5:-2] abc

3, a[0:-2] abc

从右到左取值:

原则:顾头不顾尾

1, a[2::-1] cba

2, a[-3::-1] cba

3, a[2:-6:-1] cba a[2:-5:-1] cb

 

切片规律:顾头不顾尾

 

 

若取得第二位值为最后一位用空

 

 

若从头到尾可以直接打冒号

 

 

若是为负数取切片,还是正向取值

若取值为反向取值在后边加步长

 

 

3.

 

 

默认步长为1,两个元素之间相隔几步

 

当切片反向取值时一定要加反向步长

 

 

4.字符数串的常用方法

 

 

实现大写:(或直接打印)

 

 

 

实现小写:

 

 

……开头:

b=name.startswith(“ab”)

……结尾:

c=name.endswith(“ab”)

 

 

去掉开头和结尾的空格,特定的字符:

Printa.strip())

例子

a=”&&a&bc&&”

Print(a.strip(“&”))

替换:在替换后的字符后加数字是替换的个数

 

 

 

 

字符串的方法:

1>s = "abC"

upper,lower

print(s.upper()) 实现字符串全部大写

print(s.lower()) 实现字符串全部小写

2>replace 替换

a = "abacad"

print(a.replace("a","中国"))

print(a.replace("a","中国",2)) 2 表示个数

3>capitalize,

首字母大写,其余字母小写

4>swapcase, #大小写翻转

5>strip

去掉开头和结尾的空格,特定的字符

print(a.strip())

a = "&&a&bc&&"

print(a.strip("&"))

6>startswith,endswith

结果是bool 值 ,支持切片

print(s.startswith("a")) 判断以。。。开头

print(s.endswith("a")) 判断以。。。结尾

print(s.startswith("a",1,4)) 切片部分判断

公共方法:

7>count()

s = "abac"

print(s.count("a") ) # a 元素出现的次数

8>len() print(len(s)) 对象的长度

9>split str --->list

str分割为列表,默认以空格分割

 

 

10>s.split() 默认空格

s.split("符号")

 

 

​​

11>join list ----> str

列表里面元素用指定符号进行连接

形式:”分隔符“.join(list)

 

 

join往原先的字符中添加一些东西

name.join(“*”)

12>find   字符串中字符出现的位置

例子

a = “231615”

print(a.find(“1”))       结果为2

print(a.find(“1”,3))     结果为4

print(a.find(“1”,4))     结果为-1

13>rsplit为分割符

例子

 a=6+9

print(a.rsplit(”+”))      结果为['6', '9']

 分割符中把分割的内容分开保存的方法

例子

a=6+9

print(a.rsplit(”+”))

b,c= a.rsplit(”+”,1)

print(int(a)+int(b))         结果为15

 

 

 

Guess you like

Origin www.cnblogs.com/peiw/p/11726241.html