Python学习之路——Python基础之基本数据类型

基本数据类型

  • 数字
  • 字符串
  • 列表 list
  • 元祖 tuple
  • 字典 dict
  • 布尔值 bool

×××的魔法

  1. 数字
    • 将字符串转换为数字:int
      a = "123"
      print(type(a),a)
      b = int(a)
      print(type(b),b)
      num = "0011"
      v = int(num,base=2)
      print(v)
    • bit_length
      age = 10
      r = age.bit_length()
      print(r)
  2. 字符串
    Python学习之路——Python基础之基本数据类型
    后面带等号的参数,可带可不带
    没有等号的参数,一定需要提供
    • .count 去字符串中寻找,寻找子序列的出现次数
    • .center 设置宽度,并将内容居中
    • .endswith 判断 以什么结尾
    • .find 从开始往后找,找到第一个之后,获取其位置
    • .format 格式化信息,将一个字符串中的占位符
      Python学习之路——Python基础之基本数据类型
      Python学习之路——Python基础之基本数据类型
    • .index 查找内容,找不到直接报错
    • .isalnum 字符串中是否只包含字母和数字
    • .isalpha 字符串中是否只包含字母,汉字
    • .isdecimal 字符串中是否只包含数字
    • .isdigit 字符串中是否只包含数字

Python学习之路——Python基础之基本数据类型

  • .expandtabs 断句,制表
    Python学习之路——Python基础之基本数据类型
    • .isprintable 是否存在不可显示的字符
      Python学习之路——Python基础之基本数据类型
    • .isspace 判断是否全部是空格
      Python学习之路——Python基础之基本数据类型
    • .join 将字符串中的每一个元素按照指定分隔符进行拼接
      Python学习之路——Python基础之基本数据类型
    • .ljust
    • .rjust
    • .zfill
      Python学习之路——Python基础之基本数据类型
      Python学习之路——Python基础之基本数据类型
    • .islower
    • .lower
    • .isupper
    • .upper
      Python学习之路——Python基础之基本数据类型
    • .lstrip
    • .rstrip
    • .strip
    • .strip("xx")
      Python学习之路——Python基础之基本数据类型
      Python学习之路——Python基础之基本数据类型
      Python学习之路——Python基础之基本数据类型
    • .maketrans 创建对应关系
    • .translate 按照给定的对应关系进行转换
      Python学习之路——Python基础之基本数据类型
    • .partition 进行分割 包含分割的元素
    • .rpartition
    • .split 进行分割,不包含分割的元素
    • .rsplit
    • .splitlines 进行分割,只根据换行分割
      Python学习之路——Python基础之基本数据类型
    • .startswith 判断是否以XXX开头
    • .endswith 判断是否以XXX结尾
    • .swapcase 大小写转换 aLex --> AlEX

猜你喜欢

转载自blog.51cto.com/14052013/2314417