Python Foundation "two" under

Source: Internet
Author: User
Tags iterable

Method of Str ()
String Practice

1.str.capitalize
Str.capitalize #返回首字母大写, other letter lowercase string
>>> a = ' GWDSR '
>>> a.capitalize ()
' GWDSR '
>>>
2.str.casefold
Str.casefold #字符串转换成小写 for case-insensitive string comparisons

>>> a = ' GWDSR '
>>> B = a.capitalize ()
>>> b
' GWDSR '
>>> B.casefold ()
' GWDSR '
3.str.center
Str.center #返回指定长度的字符串, the string content is centered and populated with the specified characters

>>> b.center (+, '-')
'-----------------gwdsr------------------'
4.str.count
Str.count # Returns the number of times a substring appears in a string
>>> b.count (' a ')
0
>>> b.count (' e ')
2
>>> b
' GWd SR '
5.str.encode
Str.encode #对字符串进行编码, return byte object
>>> b.encode (' GBK ')
B ' GWDSR '
6.str.endswith
Str.endswith #判断字符串是否以指定的后缀结尾
>>> b
' gwdsr '
>>> b.endswith (' R ')
True
> >> b.endswith (' d ')
False
7.str.expandtabs
Str.expandtabs #使用空格替换tab

>>> str.expandtabs (' Hello\tworld ')
' Hello World '
8.str.find
Str.find #返回子字符串在字符串中第一次出现的位置; If not found, return-1
>>> c.find (' wo ')
8
>>> C
' Hello World '
9.str.format
Str.format #执行字符串格式化操作, the replacement field is delimited with {}, and the replacement field can be a table
>>> Str.format (' Hello {} ', ' World ')
' Hello World '
10.str.format_map
Str.format_map #执行字符串格式化操作, replace field with {} delimited, with str.for
11.str.index
Str.index #同find (), but throws an error if a substring is not found in the string
>>> a = ' GWDSR '
>>> a.index (' e ')
1
>>> A.index (' a ')
Traceback (most recent):
File "<input>", line 1, in <module>
Valueerror:substring not found
12.str.isalnum
12.str.isalnum #判断字符串中是否至少有一个字符, and all characters are letters or numbers
In [202]: a = ' asf23234d '

In [203]: A.isalnum ()
OUT[203]: True

In [204]: b = ' asf '

In [205]: B.isalnum ()
OUT[205]: True

In [206]: c = ' a*sf32 '

In [207]: C.isalnum ()
OUT[207]: False

In [208]: D = '

In [209]: D.isalnum ()
OUT[209]: False
13.str.isalpha
13.str.isalpha #判断字符串中是否至少有一个字符, and all characters are letters
In [194]: A
OUT[194]: ' 123 '

In [195]: A.isa
A.isalnum A.isalpha

In [195]: A.isalpha ()
OUT[195]: False

In [196]: b
OUT[196]: ' 1a '

In [197]: B.isa
B.isalnum B.isalpha

In [197]: B.isalpha ()
OUT[197]: False

In [198]: C
OUT[198]: "

In [199]: C.isalpha ()
OUT[199]: False

in [+]: D = ' ADSF '

In [201]: D.isalpha ()
OUT[201]: True
14.str.isdecimal
14.str.isdecimal #判断字符串中是否至少有一个字符, and all characters are decimal digits
In [183]: a = ' 123 '

In [184]: b = ' 1a '

In [185]: c = "

In [189]: E = ' 1.3 '

In [the]: A.isdecimal ()
OUT[190]: True

In [191]: B.isdecimal ()
OUT[191]: False

In [192]: C.isdecimal ()
OUT[192]: False

In [193]: E.isdecimal ()
OUT[193]: False
15.str.isdigit
15.str.isdigit #判断字符串中是否至少有一个字符, and all characters are numbers
In [183]: a = ' 123 '

In [184]: b = ' 1a '

In [185]: c = "

In [186]: A.isdigit ()
OUT[186]: True

In [187]: B.isdigit ()
OUT[187]: False

In [188]: C.isdigit ()
OUT[188]: False
16.str.isidentifier
16.str.isidentifier #判断字符串中是否是有效标识符
In [172]: A
OUT[172]: ' GWDSR '

In [173]: b
OUT[173]: ' ASDF '

In [174]: A
OUT[174]: ' GWDSR '

In [175]: b = ' 12 '

In [176]: c = "

In [178]: D = ' * '

In [179]: A.isidentifier ()
OUT[179]: True

in [+]: B.isidentifier ()
OUT[180]: False

In [181]: C.isidentifier ()
OUT[181]: False

In [182]: B.isidentifier ()
OUT[182]: False
17.str.islower
17.str.islower #判断字符串中是否小字并且至少有一个字符
In [166]: a = ' GWDSR '

In [167]: b = ' ASDF '

In [168]: c = "

In [169]: A.islower ()
OUT[169]: True

In [the]: B.islower ()
OUT[170]: False

In [171]: C.islower ()
OUT[171]: False
18.str.isnumeric
18.str.isnumeric #判断字符串中是否至少有一个字符, and all characters are numeric characters
In [159]: a = ' 124 '

In []: b = ' as234 '

In [161]: c = "

In [162]: A.isnumeric ()
OUT[162]: True

In [163]: B.isnumeric ()
OUT[163]: False

In [164]: C.isnumeric ()
OUT[164]: False
19.str.isprintable
19.str.isprintable #判断字符串的所有字符都是可打印字符或字符串为空
In [151]: a = "\tpuppy"

In []: b = "Puppy\a"

In [153]: c = "Puppy"

In [154]: D = "a puppy\b"

In [155]: A.isprintable ()
OUT[155]: False

In [156]: B.isprintable ()
OUT[156]: False

In [157]: C.isprintable ()
OUT[157]: True

In [158]: D.isprintable ()
OUT[158]: False
20.str.isspace
20.str.isspace #判断字符串中是否至少有一个字符, and all characters are whitespace characters
In [144]: T2
OUT[144]: "

In [145]: T2.isspace ()
OUT[145]: False

In [146]: T3 = "

In [147]: T3.isspace ()
OUT[147]: True


In [149]: t
OUT[149]: ' GWDSR is a student '

In []: T.isspace ()
OUT[150]: False
21.str.istitle
21.str.istitle #判断字符串中是否至少有一个字符, and all characters are titlecase characters
In [135]: T = ' gwdsr is a student '

In [137]: T1 = T.title ()

In [138]: T.istitle ()
OUT[138]: False

In [139]: T1.istitle ()
OUT[139]: True

In [142]: t2 = "

In [143]: T2.istitle ()
OUT[143]: False
22.str.isupper
22.str.isupper #判断字符串中是否全部是大写字母, empty string is False
In [124]: a = ' GWDSR '

In []: A.isupper ()
OUT[125]: False

In [126]: a = ' GWDSR '

In [127]: A.isupper ()
OUT[127]: True

in [+]: b = '

In [129]: B.isupper ()
OUT[129]: False
23.str.join
23.str.join #使用字符串作为分隔符串连多个数据为一个字符串
In [%]: a
OUT[116]: ' GWDSR '

In [117]: E = [' A ', ' B ', ' C ', ' d ']

In [118]: '. Join (E)
OUT[118]: ' ABCD '

In [119]: ' * '. Join (E)
OUT[119]: ' A*b*c*d '

in [+]: ' * '. Join (a)
OUT[120]: ' P*e*t*e*r '

In [121]: '. Join (a)
OUT[121]: ' GWDSR '
# # 24.str.ljust

24.str.ljust #返回指定长度的字符串, the string content is left and populated with the specified character
In [A.ljust]: (10, '-')
OUT[88]: ' GWDSR-----'

In [the]: A.ljust (1, '-')
OUT[89]: ' GWDSR '

In [All]: A.ljust (6, ' 0 ')
OUT[90]: ' Gwdsr0 '
# # 25.str.lower

25.str.lower #字符串转换成小写
In [Bayi]: a = ' GWDSR '

In [more]: A.upper ()
OUT[82]: ' GWDSR '

In [the]: b = a.upper ()

in [+]: b
OUT[84]: ' GWDSR '

In []: B.lower ()
OUT[85]: ' GWDSR '
26.str.lstrip
26.str.lstrip #去掉字符串前面的空格, or characters in a parameter
Str.translate/str.maketrans.
Str.translate #根据table表的映射关系, converts each character in a string to another # returns a conversion table
in [+]: M = Str.maketrans (' e ', ' B ')

In [All]: A.translate (m)
OUT[33]: ' PBTBR '
28.str.partition
28.str.partition a tuple of substrings after a #返回包含字符串中分隔符之前, delimiter, delimiter
#指定的分隔符, a tuple of $3 is returned, the first is the substring to the left of the delimiter, the second is the delimiter itself, and the third is the substring to the right of the delimiter.
In [all]: g = ' www.google.com '
in [+]: g.partition ('. ')
OUT[79]: (' www ', '. ', ' google.com ')

in [+]: g.partition (' G ')
OUT[80]: (' www. ', ' g ', ' oogle.com ')
29.str.replace
29.str.replace #替换字符串中所有的子字符串old为新的字符串new
>>> a.replace (' e ', ' a ')
' Patar '
>>>
30.str.rfind #返回子字符串在字符串中最后一次出现的位置; If not found, return-1
31.str.rindex #同rfind (), but throws an error if a substring is not found in the string
32.str.rjust #返回指定长度的字符串, the string content is right and is populated with the specified character
33.str.rpartition #从后往前查找, returns the string containing the delimiter before, after the delimiter, after the delimiter
34.str.rsplit #从后往前拆分字符串, returns a list
35.str.rstrip #去掉字符串后面的空格, or characters in a parameter
36.str.split
36.str.split #拆分字符串, returns a list
in [+]: a = ' gwdsr is a student '

In [a]: A.split ()
OUT[59]: [' GWDSR ', ' is ', ' a ', ' student ']
37.str.splitlines
37.str.splitlines #字符串以换行符为分隔符拆分 to remove line breaks; if Keepends
In [si]: a
OUT[54]: ' gwdsr\n is \na student '

in [+]: print (a)
Gwdsr
Is
A student

in [+]: print (A.splitlines ())
[' GWDSR ', ' is ', ' a student ']
)
38.str.startswith
38.str.startswith #判断字符串是否以指定的前缀开始
in [+]: A
OUT[47]: ' GWDSR is a student '

In []: A.startswith (")
OUT[48]: True

in [+]: A.startswith (' a ')
OUT[49]: False
39.str.strip
39.str.strip #去掉字符串前后的空格, or all characters specified
in [+]: a = ' gwdsr is a student '

In [all]: a
OUT[42]: ' GWDSR is a student '

In []: A.strip ()
OUT[43]: ' GWDSR is a student '
40.str.swapcase
40.str.swapcase #大写字符转换成小写字符, lowercase characters are converted to uppercase characters
In []: b = a.title ()

In []: b
OUT[39]: ' GWDSR is A Student '

In [MAX]: B.swapcase ()
OUT[40]: ' GWDSR is a STUDENT '
41.str.title
41.str.title #每个单词的第一个字符转换成titlecase字符, other characters go
In [the]: a = ' gwdsr is a student '

in [+]: A.title ()
OUT[35]: ' GWDSR is A Student '
43.str.upper
43.str.upper #字符串转换成大写
in [+]: A.upper ()
OUT[26]: ' GWDSR '
44.str.zfill
44.str.zfill #在字符串的左边填充0, string not truncated
In []: A.zfill (10)
OUT[24]: ' 00000GWDSR '

in [+]: A.zfill (5)
OUT[25]: ' GWDSR '
Builtins python built-in functions
1.abs #求绝对值
2.all #判断迭代器中的所有数据是否都为true
3.any #判断迭代器中的是否有一个数据为true
4.bin #转换整数为一个二进制字符串
5.bool #转换一个数据为布尔值
6.bytearray #将数据转换为字节数组
7.bytes #将数据转换为字节数组
8.callable #判断一个对象是否可调用
9.CHR #将整数转成字符
10.classmethod #得到function的classmethod
11.compile #编译source为code或AST对象
12.complex #创建一个复数
13.delattr #删除指定的属性
14.dict #创建一个字典dictionary
15.dir #返回对象的属性列表
16.divmod #得到两个数字相除的结果和余数
17.enumerate #得到一个枚举对象
18.eval #执行一个表达式
19.exec #动态执行Python代码
20.filter #过滤数据得到一个迭代器
21.float #将字符串或数字转为浮点数
22.format #格式化数据
23.frozenset #得到新的frozenset对象
24.getattr #得到对象属性的值
25.globals #得到当前模块的全局符号表的字典
26.hasattr #判断对象是否存在属性
27.hash #得到对象的哈希值
28.help #显示帮助信息
29.hex #整数转换为十六进制表示
30.id #得到对象的id
31.input #输出提示符, read user input
32.int #将数字或字符串转为整数
33.isinstance #判断object是否是classinfo的实例
34.issubclass #判断一个类是否是另一个类的父类
35.iter #得到一个迭代器
36.len #返回对象的长度或集合的数据个数
37.list #创建一个列表
38.locals #得到当前符号表字典
39.map #更改迭代器中的每个数据得到一个新的迭代器
40.max #得到迭代器中最大的或两个或多个参数中最大的
41.min #得到迭代器中最小的或两个或多个参数中最小的
42.next #得到迭代器的下一个数据
43.object #得到object的实例
44.oct #整数转换为八进制表示
45.open #打开文件并返回一个流
46.ord #得到字符的整数表示
47.pow #乘方运算
48.print #输出数据到流
49.property #得到属性
50.range #创建一个范围对象
51.repr #得到对象的字符串表示
52.reversed #反转序列得到一个迭代器
53.round #浮点数按小数位数做舍入操作
54.set #创建一个集合对象
55.setattr #更改属性的值
56.slice #得到分片对象
57.sorted #排序可迭代的数据得到一个列表
58.staticmethod #得到function的staticmethod
59.str #得到对象的str版本
60.sum #计算可迭代数据的合计
61.tuple #创建一个元组
62.type #返回对象的类型或创建一个新的类型对象
63.vars #得到属性信息

Dict () method exercises
1.dict.clear
Dict.clear #删除dictionary中的所有key-value to

>>> a = {' K1 ': ' v1 '}
>>> A
{' K1 ': ' v1 '}
>>> A.clear ()
2.dict.copy
Dict.copy #浅拷贝dictionary

>>> B = a.copy ()
>>> b
{' K1 ': ' v1 '}
3.dict.fromkeys
Dict.fromkeys #返回一个新的dictionary, key consists of iterable elements, value equals value

>>> a = Dict.fromkeys ([' K1 ', ' K2 ', ' K3 '], ' VVV ')
>>> A
{' K3 ': ' VVV ', ' K2 ': ' vvv ', ' K1 ': ' VVV '}
4.dict.get
Dict.get #返回dictionary中key为指定值k对应的value,

>>> a.get (' K3 ')
' VVV '

>>> B = {' K1 ': a}
>>> b
{' K1 ': {' K3 ': ' VVV ', ' K2 ': ' vvv ', ' K1 ': ' VVV '}}
>>> b.get (' K1 ') [' K3 ']
' VVV '
>>>
5.dict.items
Dict.items #返回dictionary所有key-value to a set of compositions

>>> B.items ()
[(' K1 ', {' K3 ': ' VVV ', ' K2 ': ' vvv ', ' K1 ': ' VVV '})]
6.dict.keys
Dict.keys #返回dictionary所有key组成的集合

>>> B.keys ()
[' K1 ']
7.dict.pop
Dict.pop #从dictionary中删除指定key, returns the value corresponding to the specified key. If the specified key does not exist in dictionary, if D is specified, returns D, otherwise throws an exception

>>> C = b.pop (' K1 ')
>>> C
{' K3 ': ' VVV ', ' K2 ': ' vvv ', ' K1 ': ' VVV '}
>>> b
{}
>>>
8.dict.popitem
Dict.popitem #删除并返回key-value pair (key, value) as 2-tuple, if dictionary is empty, throws an exception

>>> e = C.popitem ()
>>> E
(' K3 ', ' VVV ')
>>> C
{' K2 ': ' vvv ', ' K1 ': ' VVV '}
>>>
9.dict.setdefau
Dict.setdefault #如果dictionary中不存在k, setting D[k]=d


>>> C
{' K2 ': ' vvv ', ' K1 ': ' VVV '}
>>> c.setdefault (' K1 ', ' v1 ')
' VVV '
>>> C
{' K2 ': ' vvv ', ' K1 ': ' VVV '}
>>> c.setdefault (' K4 ', ' v4 ')
' V4 '
>>> C
{' K2 ': ' vvv ', ' K1 ': ' vvv ', ' K4 ': ' V4 '}
>>>
10.dict.update
Data Update for Dict.update #使用E (dict/iterable) and F Dicti

>>> a = {' Ak1 ': ' Av1 '}
>>> B = {' BK1 ': ' Bv1 '}
>>> B.update (a)
>>> b
{' Ak1 ': ' av1 ', ' BK1 ': ' Bv1 '}
11.dict.values
Dict.values #返回dictionary所有value组成的集合
>>> b.values ()
[' Av1 ', ' bv1 ']
>>>
List ()
List Exercises

1.list.append
List.append #附加一个对象到list

>>> a = [' A ', ' B ', 1,3,4]
>>> A
[' A ', ' B ', 1, 3, 4]
>>> a.append (' d ')
>>> A
[' A ', ' B ', 1, 3, 4, ' d ']
2.list.clear
List.clear #删除list中的所有元素

>>> A.clear ()
>>> A
[]
3.list.copy
List.copy #浅拷贝list

>>> a = [' A ', ' B ', 1,3,4]
>>> A
[' A ', ' B ', 1, 3, 4]
>>> B = a.copy ()
>>> b
[' A ', ' B ', 1, 3, 4]
4.list.count
List.count #返回指定参数在list中出现的次数

>>> A.count (' B ')
1
5.list.extend
List.extend #附加指定iterable中的元素到list

>>> B = [6,6,5,4,2,4]
>>> A.extend (b)
>>> A
[' A ', ' B ', 1, 3, 4, ' d ', 6, 6, 5, 4, 2, 4]
>>>
6.list.index
List.index #返回指定值在list中第一次出现的位置, if the list has a reference

>>> A
[' A ', ' B ', 1, 3, 4, ' d ', 6, 6, 5, 4, 2, 4]
>>> A.index (1)
2
7.list.insert
List.insert #在list的指定位置index插入object

>>> A.insert (2, ' Sddsds ')
>>> A
[' A ', ' B ', ' Sddsds ', 1, 3, 4, ' d ', 6, 6, 5, 4, 2, 4]
8.list.pop
List.pop #删除并返回指定位置index的元素, default to last position, throws an error if index exceeds range or list is empty

>>> A
[' A ', ' B ', ' Sddsds ', 1, 3, 4, ' d ', 6, 6, 5, 4, 2, 4]
>>> B = A.pop ()
>>> b
4
>>> A
[' A ', ' B ', ' Sddsds ', 1, 3, 4, ' d ', 6, 6, 5, 4, 2]
>>>
9.list.remove
List.remove #从list中删除第一次出现的指定值, throws an exception if the specified value does not exist

>>> A
[' A ', ' B ', ' Sddsds ', 1, 3, 4, ' d ', 6, 6, 5, 4, 2]
>>> a.remove (' a ')
>>> A
[' B ', ' Sddsds ', 1, 3, 4, ' d ', 6, 6, 5, 4, 2]
10.list.reverse
List.reverse #反转list中的元素

>>> A
[' B ', ' Sddsds ', 1, 3, 4, ' d ', 6, 6, 5, 4, 2]
>>> A.reverse ()
>>> A
[2, 4, 5, 6, 6, ' d ', 4, 3, 1, ' Sddsds ', ' B ']
11.list.sort
List.sort #对list进行排序

>>> A
[2, 4, 5, 6, 6, ' d ', 4, 3, 1, ' Sddsds ', ' B ']
>>> A.sort ()
>>> A
[1, 2, 3, 4, 4, 5, 6, 6, ' B ', ' d ', ' Sddsds ']
>>>
Tuple ()
Meta-group Exercises

1.tuple.count
Tuple.count #返回指定参数在tuple中出现的次数

>>> b
(1, 2, 3, 4, 4, 5, 6, 6, ' B ', ' d ', ' Sddsds ')
>>> B.count (4)
2
2.tuple.index
Tuple.index #返回指定值在tuple中第一次出现的位置, if the tuple does not

>>> b
(1, 2, 3, 4, 4, 5, 6, 6, ' B ', ' d ', ' Sddsds ')
>>> B.count (4)
2
>>> B.index (2)
1
>>> B.index (1)
0
>>>

Python Foundation "two" under

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.