zoukankan      html  css  js  c++  java
  • python字符串功能学习

    1、center

    name = 'Colum'
    print(name.center(18,'*'))
    View Code

    将内容填入*的中间,效果为******Colum*******

    2.count

    计算输入参数的数量

    name = 'Colum'
    print(name.count('u'))

    3.decode 解码   encode 编码

    4.expandtabs

    将tab变为空格,也可以用 替换空格实现

    name = 'He is        2*tab'
    print(name.expandtabs())

    He is           2*tab

    5.find    index

    查找参数,返回位置。find返回失败也是有值-1, index查找 失败返回错误

    name = 'He is        2*tab'
    print(name.find('2'))
    
    返回 7
    print(name.find('3'))
    -1

    print(name.index('3'))
    ValueError: substring not found

    6.join

    连接字符串

    name = 'He is        2*tab'
    print("_".join(name))
    
    H_e_ _i_s_    _    _2_*_t_a_b

    7.partition

    将内容进行分割三部分,以参数为中间

    name = 'He is        2*tab'
    print(name.partition('2'))
    
    ('He is		', '2', '*tab')

    8.replace

    替换

    name = 'He is        2*tab'
    print(name.replace('2','3'))
    
    
    He is        3*tab

    9.split

    分割返回列表,默认空格分割

    name = 'He is        2*tab'
    print(name.split('*'))
    
    ['He is		2', 'tab']

    10.strip

    默认去除行两头的空格,参数为去除内容

    name = '22112   He is        2*tab  21  2'
    print(name.strip('2'))
    
    112   He is        2*tab  21  
  • 相关阅读:
    10-22 训练 T2 plate
    C语言I博客作业03
    c语言I博客作业02
    数学的数字
    javascript 递归
    WEB 动画的一些实现方式
    javascript 中Object一些高效的操作方法
    javascript 中Array一些高效的操作方法
    mac os 的一些命令
    javascript 继承
  • 原文地址:https://www.cnblogs.com/jonyq/p/5922135.html
Copyright © 2011-2022 走看看