字符串操作

  String:字符串放在单引号、双引号、三引号(多行时)中,从0开始索引,支持

n  查:findindex

n  切片: s[0:2]s[1:]

n  连接:“abc”+“ef” => “abcdef”join

n  分割:split

n  格式化: format

n  。。。大小写转换、编码等操作

l  list, []names=[“Dave”, “Mark”, “Ann”],列表从0开始索引,索引元素names[1]。列表可以包括任意类型的对象,可以嵌套。支持增、删、查、分片。

n  增:insertappend

n  删:delremovepop

n  查:search

n  切片: names[0:2][1:]

n  连接:[1,2,3]+[4,5] => [1,2,3,4,,5]extend

n  反转:reverse

l  tuple ()address=(“www.python.org”, 80)a = (80,)。元组语法与list相似,意义相当于枚举,可以为空,如果只含有一个元素,需要加逗号以区别于表达式(“one”, )。元组创建之后不可修改,即无法替换、删除、插入,但支持

n  索引:address[0] => www.python.org

n  切片:address[0:] => ('www.python.org', 80)

n  连接:(“www.python.org”,) + (80,) => ('www.python.org', 80)

n  host,port=addresshost => www.python.orgport => 80

l  字典: dict() {}address={"host":"www.python.org", "port":80}支持

n  索引:address[“host”] => “www.python.org”,

n  getaddress.get("host") => “www.python.org”

n  键:address.keys() => ['host', 'port']

n  in"host" in address =>True

n  删除:del address["host"] => {'port': 80}

l  集合:set()a=set([1,2,3,4]);一个数值集合 b=set(“hello”)一个唯一字符集合。与列表、元组不同,集合中的元素是无序的,无法通过数字索引,且元素不能重复。

n  并集:a|b => set([1, 2, 3, 4, 'h', 'l', 'o', 'e'])

n  交集:a&b => set([])

n  差集:a-b => set([1, 2, 3, 4]),即在a中不在b中元素

n  对称差集:a^b => set([1, 2, 3, 'e', 'h', 'l', 'o', 4])

n  adda.add(5) => set([1, 2, 3, 4, 5]) #添加一项

n  update: a.update([6,7,8]) =>set([1, 2, 3, 4, 5, 6, 7, 8])#添加多项

n  remove: a.remove(5) =>set([1, 2, 3, 4, 6, 7, 8])#删除一项

posted @ 2021-03-29 00:33  uplee  阅读(44)  评论(0编辑  收藏  举报