zoukankan      html  css  js  c++  java
  • day10,11-Python 基本数据类型介绍之数字与字符串(看看就好)

    数字:int  

    #字符串转换整型
    a = "123"
    print(type(a),a)
    b = int(a)
    print(type(b),b)
    b = b + 1000
    print(b)
    

    字符串:str

    首字母大写:capitalize()

    test = "charon"
    v =test.capitalize()
    print(v)
    

    所有变小写lower()

    test = "chAron"
    v1 = test.lower()
    print(v1)

    设置宽度,并将内容居中,20代表总长度。*代表空白位置填充,一个字符可有可无

    test = "charon"
    v2 = test.center(20,"*")
    print(v2)
    

    去字符串中寻找,寻找子序列出现的次数。可以定义起始位置 跟终止位置 

    test = "charoncccc"
    v3 = test.count("c")
    print(v3)
    

    以什么开头或者结尾startswith(),endswith()

    test = "charon"
    v4 = test.startswith("ch")
    print(v4)
    #返回True
    v5 = test.endswith("ch")
    print(v5)
    #返回False
    

    格式化,将一个字符串中得占位符替换为指定的值format()

    test = "i am {name},age {a}"
    v5 = test.format(name = "charon",a = 19)
    print(v5)
    

    高级点:

    test = "i am {0},age {1}"
    #占位符0,1,2,3...... v5 = test.format("charon",19) print(v5)

    另一种格式化,跟上面相同。利用字典格式format_map()

    test = "i am {name},age {a}"
    v5 = test.format(name = "charon",a = 19)
    v6 = test.format_map({"name":"pluto","a":20})
    print(v5)
    print(v6)
    

    判断字符串中是否只含有数字和字母isalnum()

    test = "charon123456"
    v7 = test.isalnum()
    print(v7)
    #如果真返回True,假如含有符号便返回False
    

    判断字符串是否只含字母islpha()

    test = "charon123456"
    v8 = test.isalpha()
    print(v8)
    #真返回True,假返回False
    

    判断字符串是否为数字 isdigit()

    test = "123456"
    v9 = test.isdigit()
    print(v9)
    #真返回True,假返回False
    

    判断是否是标题istitle(),title()

    test = "Return True if all cased characters in S are uppercase and there is"
    #判断是否为标题
    v10 = test.istitle()
    print(v10)
    #转化为标题
    v11 = test.title()
    print(v11)
    #再次判断是否为标题
    v12 = v11.istitle()
    print(v12)
    
    
    结果:
    /ecapp/python3.6/bin/python3.6 /opt/day10/s1.py
    False
    Return True If All Cased Characters In S Are Uppercase And There Is
    True
    
    Process finished with exit code 0
    

    将字符串的每一个元素按照指定分隔符进行拼接

    test = "iamcharom"
    v13 = "-".join(test)
    print(v13)
    
    
    结果:
    i-a-m-c-h-a-r-o-m
    

    设置宽度,并将内容居最左,右。20代表总长度,*代表空白位置填充ljust(),rjust()

    test = "charom"
    v14 = test.ljust(20,"*")
    v15 = test.rjust(20,"*")
    print(v14)
    print(v15)
    
    
    结果:
    charom**************
    **************charom
    

    判断是否全部为小写,大写和转换(有点像验证码)

    test = "Charom"
    v16 = test.islower()
    #判断是否为小写
    v17 = test.lower()
    #转换为小写
    print(v16,v17)
    v18 = v17.islower()
    #再次判断是否为小写
    print(v18)
    v19 = test.isupper()
    #判断是否为大写
    v20 = test.upper()
    #转换成大写
    print(v19,v20)
    v21 = v20.isupper()
    #再次判断是否为大写
    print(v21)
    
    
    结果:
    False charom
    True
    False CHAROM
    True
    

     默认去除左右空白、 、

    test = "   charom	"
    v22 = test.lstrip()
    print(v22)
    v23 = test.rstrip()
    print(v23)
    v24 = test.strip()
    print(v24)
    
    
    
    结果:
    charom	
       charom
    charom
    

    分割

    test = "charonchaoronaxzc"
    v25 = test.partition("a")
    v26 = test.rpartition("a")
    v27 = test.split("a")
    v28 = test.split("a",1)
    v29 = test.rsplit("a",1)
    
    print(v25)
    print(v26)
    print(v27)
    print(v28)
    print(v29)
    
    结果:
    ('ch', 'a', 'ronchaoronaxzc')
    ('charonchaoron', 'a', 'xzc')
    ['ch', 'ronch', 'oron', 'xzc']
    ['ch', 'ronchaoronaxzc']
    ['charonchaoron', 'xzc']

    替换

    test = "charoncharoncharon"
    v30 =  test.replace("ch","bbb")
    v31 = test.replace("ch","bbb",1)
    v32 = test.replace("ch","bbb",2)
    
    print(v30)
    print(v31)
    print(v32)
    
    
    结果:
    bbbaronbbbaronbbbaron
    bbbaroncharoncharon
    bbbaronbbbaroncharon

    六个基本会的

    join,split,strip,upper,lower,replace

    **********************************************************

    索引,下标,获取字符串中得某一个字符

    test = "charon"
    v = test[2]
    v1 = test[0:2]
    #>=0 <2(下标)
    v2 = test[0:-1]
    v3 = len(test)
    print(v)
    print(v1)
    print(v2)
    print(v3)
    
    结果:
    a
    ch
    charo
    6
    
    test = "charon"
    index = 0
    while index < len(test):
        print(test[index])
        index += 1
    print("=======")
    

    range    range(1,100,5)

    v = range(100)
    for item in v:
        print(item)
    test = input(">>>:")
    for item in range(0,len(test)):
        print(item,test[item])
    

      

    四个基本会的  

    for。len。索引。切片,range

     深灰魔法

    字符串一旦创建就不可修改

  • 相关阅读:
    LVDS 数据通道详解 单8 单6
    MFC中CTime获取日期时间的方法
    Sqlite 修改字段的名称。
    SQLite 字段数据类型
    把View转化成Image
    iOS 键盘类型UIKeyboardType
    验证银行卡号格式是否正确
    获取手机wifi下的网络地址
    图形处理专题(文章收集)
    收藏的博客
  • 原文地址:https://www.cnblogs.com/charon2/p/10333143.html
Copyright © 2011-2022 走看看