python3基本数据类型01——字符串

一、几个查看帮助的内置函数

  type():查看数据类型

>>> tdh = 'asdf'
>>> type(tdh) <class 'str'>

  dir():查看一个对象或类的所有属性(所有的方法名)

>>> dir(str)
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__ite
r__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'case
fold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle',
'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate',
'upper', 'zfill']

  help():查看帮助文档(详细解释)

>>> help(str.upper)
Help on method_descriptor:

upper(...)
    S.upper() -> str

    Return a copy of S converted to uppercase.

二、基本数据类型

  1、数字

    int:整型

      在32位机器上,整数的位数为32位,取值范围为-2**31~2**31-1,即-2147483648~2147483647
      在64位系统上,整数的位数为64位,取值范围为-2**63~2**63-1,即-9223372036854775808~9223372036854775807

    float:浮点型

  2、布尔型

    真或假

    0或1

  3、转义字符

    单引号:\'

    双引号:\"

    反斜杠:\\

    换行:\n

    水平制表符(在终端和文件中的输出显示相当于按下键盘TAB键效果):\t  

    空字符串,字符值为0:\0

    

  4、字符串

    格式:单引号、双引号、三个引号(‘1’、“2”、‘’‘1’‘’、“”“2222”“”)

    索引:从0开始,每一个索引对应字符串里的一个字符,可以顺着数也可以倒着数

      例:s = ’python‘

        顺着数:s[0]=='p',s[1]=='y'...s[5]==’n‘

        倒着数:s[-1]=='n'...s[-6]== 'p'

    切片:获取部分字符串,通过设置截取的初始位置和终止位置来获取想要的子字符串(s[a:b],截取的是a 到 b-1的子字符串)  注:索引是从0开始的

        例:s = ‘python’     

          想要获取'th': s[2:4]              截取的初始位置为2,终止位置为4-1=3。

    常用函数:

      capitalize(self):首字母变大写

        输入:self代表不需要输入参数

        功能:将字符串首字母大写,其它字母全变为小写

        输出:一个字符串

>>> 'tDHG'.capitalize()
'Tdhg'
          
      upper(self):全部变大写
        输入:无
        功能:将字符串变为大写
        输出:输出改为大写的字符串
>>> 'python'.upper()
'PYTHON'
      lower(self):变小写
        输入:无
        功能:将字符串变为小写
        输出:输出改为小写的字符串
>>> 'PYTHON'.lower()
'python'
      strip(self, chars=None):(rstrip清空左侧,lstrip清空右侧)清空左右
        输入:chars清需要从字符串左右两边清空的字符
        功能:清除字符串左右两侧多余的子字符
        输出:输出清空后的字符串
>>> 'htdhtdh'.strip('h')
'tdhtd'
>>> '  tdh '.strip()
'tdh'     
      ljust(self, width, fillchar=None):(rjust字符串置于右边,填充字符置于左边)填充字符
        输入:width为输出字符宽度,fillchar表示填充的字符,默认填充字符为空格

          功能:将字符串置于左边位置,并根据第二个参数在右边添加字符

          输出:输出一个长为width的字符串,左边为字符串,右边为填充字符

>>> 'tdh'.ljust(5,'*')
'tdh**'
>>> 'tdh'.rjust(5,'*')
'**tdh'
      center(self, width, fillchar=None):填充字符
        
输入:width为宽度,fillchar表示两边填充的字符,默认填充字符为空格

          功能:将字符串置于中心位置,并根据第二个参数在两边添加字符

          输出:输出一个长为width的字符串,中心为字符串,两边为填充字符

>>> 'python'.center(10,'*')
'**python**'
      count(self, sub, start=None, end=None):查找子字符串个数
        输入:sub需要查找的子字符串,start开始查找的位置,end查找结束位置的后一个位置
        功能:计算子字符串sub在字符串中的个数
        输出:输出子字符串在查找范围内的个数
>>> 'python'.count('th')
1

        find(self, sub, start=None, end=None):(rfind表示从右边开始—)找子字符串位置

         输入:sub查找的子字符串,start开始查找的位置,end查找结束位置的后一个位置

          功能:从左边开始查找子字符串 

          输出:输出子字符串第一次出现的位置,若没有找到则返回-1

  >>> 'tdhtdh'.find('t')
  0

  >>> 'python'.find('l')
  -1

        index(self, sub, start=None, end=None):找子字符串位置

          输入:sub查找的子字符串,start开始查找的位置,end查找结束位置的后一个位置

          功能:从左边开始查找子字符串 

          输出:输出子字符串第一次出现的位置,若没有找到就报错

>>> 'python'.index('p')
0
>>> 'python'.index('l')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: substring not found
      partition(self, sep):分割字符串(保留分隔符)
        输入:sep分割字符(以参数sep参数为分割符,分为sep之前,色谱本身,sep之后)
        功能:将字符串分割
        输出:输出一个元祖,其中包含sep分隔符之前部分,sep之后部分,sep本身(若没有找到分隔符,则返回字符串以及两个空字符串)
>>> 'python'.partition('th')
('py', 'th', 'on')
>>> 'python'.partition('l')
('python', '', '')

      split(self, sep=None, maxsplit=-1):分割字符串(不保留分隔符)

        输入:sep为分割符(当sep为None或没有设置时,默认以空格作为分隔符同时将其它空格都移除),maxsplit为最多分割次数(默认为-1即遇到分隔符就分割一次,直到没有不存在分隔符为止)

        功能:以分隔符将字符串分割为左右两边

        输出:输出一个列表,包含除分隔符以外的所有子字符串

>>> '  pyth on  '.split()
['pyth', 'on']
>>> '  pyth on  '.split('th')
['  py', ' on  ']
>>> g = 'tdh'*5
>>> g.split('d',-1)
['t', 'ht', 'ht', 'ht', 'ht', 'h']
>>> g.split('d',2)
['t', 'ht', 'htdhtdhtdh']

      join(self, iterable):连接

        输入:iterable表示需要连接的参数

        功能:将iterable参数中的元素连接起来

        输出:输出一个以指定字符为连接符,将iterable参数连接起来的字符串

>>> t = ('a','b','c')
>>> s = ('-')
>>> s.join(t)
'a-b-c'
>>> q =' python'
>>> s.join(q)
' -p-y-t-h-o-n'
>>> l = ['a','b','c']
>>> s.join(l)
'a-b-c'

  

猜你喜欢

转载自www.cnblogs.com/tandonghao/p/9101418.html