zoukankan      html  css  js  c++  java
  • 内置函数 字符串操作

    print cmp(2,3) 
    print cmp(2,2) 
    print cmp(3,2) 
    abs(-1)
    print bool(0) 
    print bool(-1)      False
    print bool('')       False
    print divmod(3,2)
    print min([11,22,33,44])   #必须是列表
    print max([11,22,33,44])   #必须是列表
    ​print pow(2,10)            #2的10次幂  
    print len('i am abcd')     #字符串的长度
    ​print all([1,2,3,4])   值是 True ,遍历所有值后,如果是真才为True
    print all([1,2,3,0])   值是 false,遍历所有值后,如果是真才为false
    print any([1,2,3,0])  值为true,如果有一个真,就为true
    print any([0,0,0,0])  如果全为假,值是false
    ​print chr(65) 值: A
    print ord('a')  值: 97
    ​##################################################################################
    enumerate  加序号
    list1 = ['汽车','火车','飞机']
    for k,v in enumerate(list1,1)
        print k,v         
    值为enumerate(list,1),显示从1开始
    1 汽车
    2 火车
    3 飞机
    ///////////////////////////////////////////////////
    for item in enumerate(list1,1): 
        print item[0],item[1]
    值为enumerate(list,1),显示从1开始
    1 汽车
    2 火车
    3 飞机
    ---------------------------------------------------
    字符串的格式化
    
    s = 'I am {0} {1}' 
    print s.format('fengjian','ok')
    ----------------------------------------------------------
    map应用
    li = [111,222,333]
    temp = [] 
    第一种法方法
    for item  in  li:
        temp.append(item + 100)
    print temp
    ////////////////////////
    第二种方法
    def foo(arg): 
        return arg + 100  
    for item in li:
        temp.append(foo(item))
    print temp
    ///////////////////////////////
    def foo(arg): 
        return arg + 100  ​
    print map(foo,li)
    ///////////////////////////////
    ​print map(lambda x:x+100,li)
    ​
    ​
    ​name = "feng"

    print(name.capitalize()) 首字母大些

    print(name.count("e")) 字符串中有e的数量

    print(name.center(50,'-')) 一共50个字符,name放在中间,左右用 - 补全
    ---------------------fengjian---------------------


    print(name.endswith("g")) #判断字符串是否以g结尾,返回值是true或者false
    true

    print(name.find("g")) #查找字符串中 g的索引 ​
    name = "my name is {name} and  age is {age}"      #格式化输出
    print(name.format(name="feng",age=30))
    print(name.format_map({'name':'feng','age':'30'}))    #字典

    print('+'.join(["a","b","c"])) #字符串拼接join
    a+b+c

    print(name.ljust(50,'*')) 一共50个字符,左侧字符串,不够50个字符用 * 补全

    print(name.rjust(50,'*')) 一共50个字符,右侧字符串,不够50个字符用 * 补全

    print('Feng'.lower()) 大些变小写

    print('feng',upper()) 小写变大写

    print(' feng '.strip) 去掉空格和回车

    print('feng'.replace('n','N'))  替换
    print('feng jian'.split())    把字符串按照空格分割成列表
    print('1+2
    +3+4'.splitlines())   按照换行分割


  • 相关阅读:
    安装MySQLdb
    树莓派及其他硬件平台国内外Linux镜像站全汇总
    rpc使用举例
    SAE上安装第三方模块
    【Java】Map
    【Java】判断字符串是否含字母
    【Android Studio】提示代码忽略大小写
    【iOS】Xcode 离线文档
    【iOS】iOS main() 简介
    【eclipse】No enclosing instance of type A is accessible. Must qualify the allocation with an enclosing instance of type A
  • 原文地址:https://www.cnblogs.com/fengjian2016/p/5156998.html
Copyright © 2011-2022 走看看