• Python 基础【二】 下


      str()的方法
      字符串练习

      1.str.capitalize
      str.capitalize #返回首字母大写,其他字母小写的字符串
      >>> a = 'gwdsr'
      >>> a.capitalize()
      'gwdsr'
      >>>
      2.str.casefold
      str.casefold #字符串转换成小写,用于不区分大小写的字符串比较

      >>> a = 'gwdsr'
      >>> b = a.capitalize()
      >>> b
      'gwdsr'
      >>> b.casefold()
      'gwdsr'
      3.str.center
      str.center #返回指定长度的字符串,字符串内容居中,并使用指定字符填充

      >>> b.center(40,'-')
      '-----------------gwdsr------------------'
      4.str.count
      str.count #返回子字符串在字符串中出现的次数
      >>> b.count('a')
      0
      >>> b.count('e')
      2
      >>> b
      'gwdsr'
      5.str.encode
      str.encode #对字符串进行编码,返回字节对象
      >>> 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 world')
      'hello world'
      8.str.find
      str.find #返回子字符串在字符串中第一次出现的位置;如没找到,返回-1
      >>> c.find('wo')
      8
      >>> c
      'hello world'
      9.str.format
      str.format #执行字符串格式化操作,替换字段使用{}分隔,替换字段可以是表
      >>> str.format('hello {}','world')
      'hello world'
      10.str.format_map
      str.format_map #执行字符串格式化操作,替换字段使用{}分隔,同str.for
      11.str.index
      str.index #同find(),但如果在字符串中没找到子字符串,会抛出错误
      >>> a = 'gwdsr'
      >>> a.index('e')
      1
      >>> a.index('a')
      Traceback (most recent call last):
      File "<input>", line 1, in <module>
      ValueError: substring not found
      12.str.isalnum
      12.str.isalnum #判断字符串中是否至少有一个字符,并且所有字符都是字母或数字
      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 #判断字符串中是否至少有一个字符,并且所有字符都是字母
      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 [200]: d = 'adsf'

      In [201]: d.isalpha()
      Out[201]: True
      14.str.isdecimal
      14.str.isdecimal #判断字符串中是否至少有一个字符,并且所有字符都是十进制数字
      In [183]: a = '123'

      In [184]: b = '1a'

      In [185]: c = ''

      In [189]: e = '1.3'

      In [190]: 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 #判断字符串中是否至少有一个字符,并且所有字符都是数字
      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 [180]: 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 [170]: b.islower()
      Out[170]: False

      In [171]: c.islower()
      Out[171]: False
      18.str.isnumeric
      18.str.isnumeric #判断字符串中是否至少有一个字符,并且所有字符都是数字字符
      In [159]: a = '124'

      In [160]: 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 = " Puppy"

      In [152]: b = "PUPPYa"

      In [153]: c = "puppy"

      In [154]: d = "a puppy"

      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 #判断字符串中是否至少有一个字符,并且所有字符都是空白字符
      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 [150]: t.isspace()
      Out[150]: False
      21.str.istitle
      21.str.istitle #判断字符串中是否至少有一个字符,并且所有字符都是titlecase字符
      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 #判断字符串中是否全部是大写字母,空字符串为false
      In [124]: a = 'gwdsr'

      In [125]: a.isupper()
      Out[125]: False

      In [126]: a = 'gwdsR'

      In [127]: a.isupper()
      Out[127]: True

      In [128]: b = ''

      In [129]: b.isupper()
      Out[129]: False
      23.str.join
      23.str.join #使用字符串作为分隔符串连多个数据为一个字符串
      In [116]: 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 [120]: '*'.join(a)
      Out[120]: 'p*e*t*e*r'

      In [121]: ''.join(a)
      Out[121]: 'gwdsr'
      ### 24.str.ljust

      24.str.ljust #返回指定长度的字符串,字符串内容居左,并使用指定字符填充
      In [88]: a.ljust(10,'-')
      Out[88]: 'gwdsr-----'

      In [89]: a.ljust(1,'-')
      Out[89]: 'gwdsr'

      In [90]: a.ljust(6,'0')
      Out[90]: 'gwdsr0'
      ### 25.str.lower

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

      In [82]: a.upper()
      Out[82]: 'gwdsR'

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

      In [84]: b
      Out[84]: 'gwdsR'

      In [85]: b.lower()
      Out[85]: 'gwdsr'
      26.str.lstrip
      26.str.lstrip #去掉字符串前面的空格,或参数中的字符
      27. str.translate / str.maketrans
      str.translate #根据table表的映射关系,将字符串中的每个字符转换成另一个#返回一个转换表
      In [32]: m = str.maketrans('e','b')

      In [33]: a.translate(m)
      Out[33]: 'pbtbr'
      28.str.partition
      28.str.partition #返回包含字符串中分隔符之前、分隔符、分隔符之后的子字符串的tuple
      #指定的分隔符,则返回一个3元的元组,第一个为分隔符左边的子串,第二个为分隔符本身,第三个为分隔符右边的子串
      In [77]: g = 'www.google.com'
      In [79]: g.partition('.')
      Out[79]: ('www', '.', 'google.com')

      In [80]: 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 #返回子字符串在字符串中最后一次出现的位置;如没找到,返回-1
      31.str.rindex #同rfind(),但如果在字符串中没找到子字符串,会抛出错误
      32.str.rjust #返回指定长度的字符串,字符串内容居右,并使用指定字符填充
      33.str.rpartition #从后往前查找,返回包含字符串中分隔符之前、分隔符、分隔符之后
      34.str.rsplit #从后往前拆分字符串,返回一个列表
      35.str.rstrip #去掉字符串后面的空格,或参数中的字符
      36.str.split
      36.str.split #拆分字符串,返回一个列表
      In [58]: a = ' gwdsr is a student '

      In [59]: a.split()
      Out[59]: ['gwdsr', 'is', 'a', 'student']
      37.str.splitlines
      37.str.splitlines #字符串以换行符为分隔符拆分,去掉换行符;如果keepends
      In [54]: a
      Out[54]: 'gwdsr is a student'

      In [55]: print(a)
      gwdsr
      is
      a student

      In [56]: print(a.splitlines())
      ['gwdsr', ' is ', 'a student']
      )
      38.str.startswith
      38.str.startswith #判断字符串是否以指定的前缀开始
      In [47]: a
      Out[47]: ' gwdsr is a student '

      In [48]: a.startswith(' ')
      Out[48]: True

      In [49]: a.startswith('a')
      Out[49]: False
      39.str.strip
      39.str.strip #去掉字符串前后的空格,或指定的所有字符
      In [41]: a = ' gwdsr is a student '

      In [42]: a
      Out[42]: ' gwdsr is a student '

      In [43]: a.strip()
      Out[43]: 'gwdsr is a student'
      40.str.swapcase
      40.str.swapcase #大写字符转换成小写字符,小写字符转换成大写字符
      In [38]: b = a.title()

      In [39]: b
      Out[39]: 'gwdsr Is A Student'

      In [40]: b.swapcase()
      Out[40]: 'gwdsR iS a sTUDENT'
      41.str.title
      41.str.title #每个单词的第一个字符转换成titlecase字符,其他字符转
      In [34]: a = 'gwdsr is a student'

      In [35]: a.title()
      Out[35]: 'gwdsr Is A Student'
      43.str.upper
      43.str.upper #字符串转换成大写
      In [26]: a.upper()
      Out[26]: 'gwdsR'
      44.str.zfill
      44.str.zfill #在字符串的左边填充0,不会截断字符串
      In [24]: a.zfill(10)
      Out[24]: '00000gwdsr'

      In [25]: a.zfill(5)
      Out[25]: 'gwdsr'
      builtins Python内置函数
      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 #输出提示符,读取用户输入
      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() 方法练习
      1.dict.clear
      dict.clear #删除dictionary中的所有key-value对

      >>> 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由iterable的元素组成,value等于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对组成的集合

      >>> 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,返回指定key对应的value。如果dictionary中不存在指定key,如果指定了d,返回d,否则抛出例外

      >>> c = b.pop('k1')
      >>> c
      {'k3': 'vvv', 'k2': 'vvv', 'k1': 'vvv'}
      >>> b
      {}
      >>>
      8.dict.popitem
      dict.popitem #删除并返回key-value对(key, value)作为2-tuple,如果dictionary为空,抛出例外

      >>> e = c.popitem()
      >>> e
      ('k3', 'vvv')
      >>> c
      {'k2': 'vvv', 'k1': 'vvv'}
      >>>
      9.dict.setdefau
      dict.setdefault #如果dictionary中不存在k,设置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
      dict.update #使用E(dict/iterable)和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()
      列表练习

      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中第一次出现的位置,如果list上存在指

      >>> 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的元素,默认为最后位置,如果index超过范围或list为空,抛出错误

      >>> 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中删除第一次出现的指定值,如果指定值不存在,抛出例外

      >>> 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()
      元组练习

      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中第一次出现的位置,如果tuple中不

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

  • 相关阅读:
    关于MySQL5.6配置文件my-default.ini不生效问题
    jQuery学习总结(三)
    jQuery学习总结(二)
    jQuery学习总结(一)
    mysql输出到页面MVC模式
    简单的在jsp页面操作mysql
    mysql5.7的基本使用
    mysq5.7l的下载与配置
    jdk环境变量的配置
    SQL SERVER——给已有数据的表增加唯一值
  • 原文地址:https://www.cnblogs.com/sunface/p/5163949.html
Copyright © 2020-2023  润新知