zoukankan      html  css  js  c++  java
  • python 基本数据类型

    python的基本数据类型

    int 类型

     1 #   int  
     2 #   将字符串转换成数字  
     3 #   a='123a' 不能改  
     4 a = "123"  
     5 b = int(a)  
     6 print(b)  
     7 print(type(b))  #   查看一个元素的类型  
     8   
     9 #   这里转换的是ascii值, 如果是多个字符就不能转换了  
    10 a = 'b'  
    11 b = int(b)  
    12 print(b)     
    13    
    14 #   进制转换, 转换成2进制,  
    15 num = "0011"  
    16 v = int(num, base=2)  
    17 print(v)  
    18   
    19 #   当前数字的二进制,至少用几位表示  
    20 age = 10  
    21 r = age.bit_length()  
    22 print(r) 

    range 方法

    1 #   创建连续数字, 从7到2, 每次减去2,, 7, 5, 3  
    2 v = range(7, 1, -2)  #  -2是步长  
    3 for i in v:  
    4     print(i) 

    常用字符串  方法

    test="hello world"  
    #   replace, 替换("aa", 'b'), aa换成b, 最后面可以加替换几个  
    #   常用 join, split, find, strip, upper, lower, replace必须会的  
    test = '你是风儿我是沙'  
    print(test)  
    t = '*'  
    v = t.join(test)  
    print(v)  # 每个里面多有个空格  
    
    结果
    你是风儿我是沙 -- >  你*是*风*儿*我*是*沙 

    split  分割字符串

    #   分割字符串  
    # split(self, sep=None, maxsplit=-1)  
    test = "absadfsfff"  
    #   全部分割, 如果后面加上参数, 就是分割几次  
    v2 = test.split('s')   #    按照s分割  
    print(v2, type(v2))  
    
    结果:
    ['ab', 'adf', 'fff'] <class 'list'> 
    View Code

    find 查找子串

    #   查找子串第一次出现的位置, 还有个rfind是从后向前找, 找不到返回-1  
    # find(self, sub, start=None, end=None)  
    test = "hello"  
    v = test.find('lo')  
    print(v)  
    View Code

    replace 替换

    #   最后一个参数是替换的次数  
    # replace(self, old, new, count=None)  
    test = "hello"  
    v = test.replace("ll", 'tt')  
    print(v)   
    #   结果  
    # hetto  
    View Code

    strip 去除空白,或其他字符

    lstrip(self, chars=None)  
    #   去除左右空白, 包括换行  
    #   或者移除指定字符  
    test = '
    Alex '  
    l_v = test.lstrip()  
    r_v = test.rstrip()  
    v = test.strip()  
    print(l_v, r_v, v)  
    
    结果:
    Alex    
    Alex Alex  
    View Code

    upper 和lower 换成大写或小写

    test  = "hello"  
    v = test.upper()  
    print(v)  # 全为大写  
    v = test.lower()  
    print(v)  # 全为小写 
    View Code

    切片

     
    #   获取字符串中的某一个字符, 某几个 用切片  
    test = "abcde"  
    v = test[0]  
    v2 = test[1:2]  #   不包括2  
    v3 = test[:-1]  #   后面的第一个  
    #   字符串长度  
    v4 = len(test)  
      
    #   字符串一旦创建就不可修改,  
    #   一旦修改或者拼接 就会生成新的字符串(开辟新的空间),很多语言都是的  
    View Code

    其他了解的方法

    # #   大小写转换  
    # test = "hello WORLD"  
    # v = test.swapcase()  
    # print(v)....还有很多, 看其源码  
    #   判断 字符串中是否 只包含   字母或数字,
    #   是否是字母和汉字,,  是否是数字   islower 是否是小写,
    # isalnum(), isalpha,  isdecimal  isdigit  isidentifier  islower ,
    # isnumeric  isprintable  isspace  istitle  isupper
    View Code
  • 相关阅读:
    DRF之url注册器组件
    序列化组件的使用及接口设计和优化
    Django 内置字段
    Django 的 ModelForm组件
    Django组件 中间件
    csrf
    django使用redis做缓存
    微信消息推送
    自定制serilazry字段
    小知识,大智慧(restframework 拾忆)
  • 原文地址:https://www.cnblogs.com/xiaokang01/p/9010494.html
Copyright © 2011-2022 走看看