zoukankan      html  css  js  c++  java
  • python string

    通过例子讲解python是最好的办法,利于学习,利于记忆,做好笔记

    string.replace(str, old, new[, maxreplace])
    import string
    s='123456789123'
    print s
    #替换算法 string.replace(s, old, new, maxreplace) print string.replace(s, '123', '*****') print string.replace(s, '123', '*****',1)
    print s.replace('123',"fuck")

    #输出如下:
    #123456789123
    #*****456789*****
    #*****456789123
    #fuck456789fuck
    string.zfill(s, width)
    import string
    print string.zfill("12345", 6)
    print string.zfill("-1234", 6)
    print string.zfill("123456", 6)
    

     输出:

    012345
    -01234
    123456

    string.strip(s[, chars])
    import string
    print string.strip('   hello world   ')
    print string.lstrip('   hello world   ')
    print string.rstrip('   hello world   ')
    print string.strip('***hello world***','*')
    print string.strip('hello world','world')

    输出:

    In [1]: import string

    In [2]: string.strip(' hello world ')
    Out[2]: 'hello world'

    In [3]: string.strip(' hello world','world')
    Out[3]: ' hello '

    In [4]: string.strip('   hello world   ')
    Out[4]: 'hello world'

    In [5]: string.lstrip('   hello world   ')
    Out[5]: 'hello world   '

    In [6]: string.rstrip('   hello world   ')
    Out[6]: '   hello world'

    In [7]: string.strip('***hello world***','*')
    Out[7]: 'hello world'

    In [8]: print string.strip('hello world','world')
    hello

  • 相关阅读:
    Sip协议中的严格路由和松路由
    读书有感(转)
    c# ini文件操作类(简单配置文件)
    android ApiDemos学习1 主界面动态ListView显示
    android 长度单位
    ArcGIS Engine 常用方法
    android simcard
    android 屏蔽home键操作
    android activity
    android ListView
  • 原文地址:https://www.cnblogs.com/UnGeek/p/2990946.html
Copyright © 2011-2022 走看看