20190802 numeric types and string types built-in method

Besides built-in method before, first to introduce what is orderly or disorderly, what is variable or immutable

Ordered: an index

Disorder: None Index

Variable: The value of constant change id, not a hash

Immutable: variable id value becomes, hashable

01 built-in digital type method

Plastic

  1. Action (refer to particular types of data base)

    Description of age, id

  2. Defined manner (refer to particular types of data base)

    You may be used int (), the purely digital string to decimal integer

    age = 19  # age = int(10)
    print(type(age))
    #<class 'int'>
    x = int('111')
    print(type(x))
    #<class 'int'>
    x = int('11.1')  # 报错
    print(x)
  3. Built-in method

    No built-in method, only arithmetic + - * / and compare operations> <> = <=

  4. A plurality of stored value or values

    A value which is not nonsense it? A variable name corresponds to a variable value

  5. Ordered or unordered

    They not say

  6. Variable or non-variable (Key)

    Digital type is immutable

Float

  1. Action (refer to particular types of data base)

    Uses: Payroll, height, weight

  2. Defined manner (refer to particular types of data base)

    You can float () method of the string to a purely digital floating-point number.

    age = 3.1  # age = float(3.1)
    print(type(age))
    #<class 'float'>
    x = float('111')
    print(x)
    print(type(x))
    111.0
    #<class 'float'>
    x = float('11.1')  # 报错
    print(type(x))
    #<class 'float'>
  3. Built-in method

    + _ Arithmetic comparison operation

  4. A plurality of stored value or values

    A value

  5. Ordered or unordered

    No one said an orderly or disorderly

  6. Variable or non-variable (Key)

    Immutable data type

02 built-in string type method

  1. Action (refer to particular types of data base)

    Describe the nature of things, such as the person's name, a single loving, address, country, etc.

  2. Defined manner (refer to particular types of data base)

    Use '', '', '' '' '', "" "" "" wrapped a string of characters

    • u'unicode ': unicode string encoded
    • b'101 ': binary coded string
    • r '\ n': original string, that is to say '\ n' which is an ordinary two characters, line feed and meaningless
    name = 'nick'  # name =str('nick')
    s1 = str(1.1)
    s2 = str([1, 2, 3])
    
    print(f's1:{s1}, type:{type(s1)}')
    print(f's2:{s2}, type:{type(s2)}')
    #s1:1.1, type:<class 'str'>
    #s2:[1, 2, 3], type:<class 'str'>

    See here's the big brothers welfare, and this in terms of a binary type Print

    s = b'sdkfljl' # 打印出来的bytes类型,二进制类型,010101010100110100  # 有这种二进制的定义方式,其实一点用都没有
    print(s)

    In terms of several escape character

    \n #换行
    s  = 'a\na'  # 碰到斜杠了,计算机就清楚下一个字符和\拼接在一起会有特殊的意义
    print(s)
    
    \t 缩进4个空格
    s = 'a\t\ta'
    print(s)
    
    \r 回退上一个打印结果,覆盖上一个打印结果
    print('\\ra',end='')  # 加一个\让后面的\变得无意义
    print('\\ra',end='')
    
    
    s = 'a\\na'
    print(s)
    s = r'\ra\t\na'  # raw 去掉转义字符,等于使\没有意义
    print(s)
    

    In terms of a built-in packages os, for splicing path

    path = r'D:'
    print(path)
    import os
    path = os.path.join(path,'test.txt')
  3. String built-in method (only string type to use)

    Priority grasp

    1. index value
    print(s[1])
    2. Slice
    print(s[4:0:1])  # 1表示从左到右
    
    print(s[-4::-1])  # -1表示从右到左  # 不推荐掌握
    
    print(s[4:0:-1])  # -1表示从右到左
    
    s = 'Thousand_Mesh handsome'
    3.for cycle
    for i in s:
        print(i)
    4.strip()
    s1 = '      Thousand_Mesh handsome         '
    print(s1.strip())  # 去两端的空白
    
    s2 = '***!!!!!Thousand_Mesh handsome----***'
    print(s2.strip('-*!'))  # 指定多个字符一起去掉,只要strip里面有的字符就全部干掉
    print(s2.strip('nick'))  # 指定多个字符一起去掉,只要strip里面有的字符就全部干掉
    
    s2.strip('*-!')  # 首先判断字符串s的两端字符,为*,再去strip里找有没有*,有就去掉,再去判断字符串s的两端字符,!-,再从strip里面找,有去掉,没有停止去掉
    
    print(s2)
    
    print('*' * 50)
    s2 = '***!!!!!Thousand_Mesh handsome----***'
    5.split () Cutting
    print(s2.split())  # 默认以空格切割字符串
    print(s2.split('!'))  # 以!切割
    print(s2.split('!', 2))
    
    s2 = '***!!!!!Thousand_Mesh handsome----***'
    6.in 或 not in
    print('*' in s2)  # True
    print('$' not in s2)  # True
    7. length len
    s2 = 'Thousand_Mesh handsome'
    print(len(s2))  # 求字符串的长度

    Need to know

    1.lstrip () and rstrip ()
    s2 = '***!!!!!Thousand_Mesh handsome----***'
    print(s2.lstrip('*'))
    print(s2.rstrip('*'))
    2.rsplit ()
    print(s2.split('*', 1))
    print(s2.rsplit('*', 1))
    3.lower&upper
    s3 = 'aaabbJ'
    print(s3.lower())
    print(s3.upper())
    4.startswith&endswith
    s3 = 'aaabbJ'
    print(s3.startswith('b'))
    print(s3.endswith('J'))
    5.join (with more) and generally associated with split
    s3 = ' '
    print(s3.join(['234', '234', '234']))  # 以s3为间隔符,拼接列表里的每一个元素
    
    s = '辣条/薯片/汽水/泡面/火腿肠/枸杞/当归/鹿茸'
    
    s1 = s.split('/')
    print('*'.join(s1))
    6.replace
    s2 = 'Thousand_Mesh handsome'
    
    print(s2.replace('Thousand_Mesh', 'tt'))
    7.isdigit (purely digital) / isalpha (plain letters)
    s2 = '12312'
    print(s2.isdigit())
    
    s3 = 'aaac1c'
    print(s3.isalpha())
    
    username_db ='nick'
    
    pwd_db = '123'
    
    inp_username = input('username:')
    
    inp_pwd = input('pwd:')
    
    if inp_username.isalpha():
    
    pass
    
    if inp_pwd.isdigit():
    
    pass

    Understand (can can not can not)

    1.find|rfind|index|rindex|count
    s2 = '**23423***ni234234ck $$ hand223423some******'
         01234567891011
    print(s2.find('$'))  # 从左找,找到第一个停止,找不到返回-1
    print(s2.rfind('$'))  # 从右找,找到就停止,找不到返回-1
    print(s2.index('$'))  # 找不到报错
    print(s2.rindex('$'))  # 找不到报错
    2.center | bright | rjust | zfill
    s2 = 'nick handsome'
    print(s2.center(50, '*'))  # 居中
    print(s2.ljust(50, '*'))  # 居左
    print(s2.rjust(50, '*'))  # 居右
    print(s2.zfill(50))  # 填充0居右
    3.expandtabs
    s2 = 'a\ta'
    print(s2)
    print(s2.expandtabs(8))  # 针对\t而言
    4.captalize | swapcase | title only for English
    s2 = 'harry Potter'
    print(s2.capitalize())  # 首字母(一句话的开头)大写,其他全小写,用在段落开始
    print(s2.swapcase())  # 大小写互换
    print(s2.title())  # 所有单词首字母大写
    5.is series (they know who are interested)
  4. A plurality of stored value or values

    A value

  5. Ordered or unordered

    Ordered

  6. Variable or non-variable (Key)

    Immutable

Guess you like

Origin www.cnblogs.com/TMesh-python/p/11291151.html