zoukankan      html  css  js  c++  java
  • python学习-day11-内建函数

    python-内建函数

    -int:将字符串转换为数字

    a = "123"
    print(type(a),a)
    b = int(a)
    print(type(b),b)
    num = "0011" 
    v = int(num, base=16)
    print(v)

    - bit_lenght:# 当前数字的二进制,至少用n位表示

    age=5
    r = age.bit_length()
    print(r)

    字符串:str

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

    test = "你是风儿我是沙"
    print(test)
    t = ' '
    v = t.join(test)
    print(v)
    你是风儿我是沙
    你 是 风 儿 我 是 沙
    答案

    2、split、rsplit #分割为指定个数

    name = " aleX"   
    v=name.split("l")
    print(v)
    [' a', 'eX']
    答案

    3、find;从开始往后找,找到第一个之后,获取其位置。找不到:-1

    name = " aleX"
    v=name.find(1)
    print(v)
    
    name = " aleX"
    v=name.find("e")
    print(v)
    -1
    3
    答案

    3.1、index:查找指定字符。找不到则报错!返回指定字符所在的序列。

    test = "alexalex"
    v = test.index('8')
    print(v)

    test = "alexalex"
    v = test.index('a')
    print(v)
        v = test.index('8')
    ValueError: substring not found
    View Code
    --0
    View Code

    4、strip、lstrip、rstrip----------移除制定字符串,有限先最多匹配

    test = "xadsds"
    a = test.lstrip('xa')
    print(a)
    b = test.rstrip('lexxexa')
    print(b)
    c = test.strip('xas')
    print(c)
    dsds
    xadsds
    dsd
    答案

    什么都不填的时候,去除左右空白

    name = " aleX "   
    v= name.strip()
    print(v)
    aleX
    View Code
    name = " aleX "   
    v= name.strip()
    print(v)
    a=name.rstrip()
    print(a)
    b=name.lstrip()
    print(b)
    aleX
     aleX
    aleX 
    答案

    # 去除
    # v = test.lstrip()
    # v = test.rstrip()
    # v = test.strip()
    # print(v)


    5、upper、lower变成大小写、isupper、islower,是否全部大小写

    test = "Alex"
    v1 = test.islower()
    v2 = test.lower()
    print(v1, v2)
    
    v1 = test.isupper()
    v2 = test.upper()
    print(v1,v2)
    False alex
    False ALEX
    答案

    6、casefold 所有字符变为小写,并且更加牛逼。很多未知的相应变小写

    test="Akkis"
    v1 = test.casefold() print(v1) v2 = test.lower() print(v2)
    akkis@1
    akkis@1
    View Code

    7、replace:将指定字符串替换为指定字符串。

    test = "alexalexalex"
    v = test.replace("ex",'bbb')
    print(v)
    v = test.replace("ex",'bbb',1)
    print(v)
    albbbalbbbalbbb
    albbbalexalex
    答案

    8、capitalize:首字母变为大写

    a = "alex"
    b = a.capitalize()
    print(a)
    print(b)
    alex
    Alex
    答案
  • 相关阅读:
    基于边缘保留滤波实现人脸磨皮的算法 | 掘金技术征文
    图像算法---表面模糊算法
    通过人脸照片更换皮肤的方法及系统
    一种数字图像自动祛除斑点的方法
    Leetcode 301.删除无效的括号
    Leetcode 300.最长上升子序列
    Leetcode 299.猜字游戏
    Leetcode 297.二叉树的序列化和反序列化
    Leetcode 295.数据流的中位数
    Leetcode 289.生命游戏
  • 原文地址:https://www.cnblogs.com/laixiaoyun/p/6096077.html
Copyright © 2011-2022 走看看