zoukankan      html  css  js  c++  java
  • 03-Python里字符串的常用操作方法二

    1、lstrip():删除左侧空白字符

    实例:

    my_str = '   hello world and my and test and python   '  # 原始字符串
    print(my_str)
    # lstrip() 删除左侧空白字符
    my_str1 = my_str.lstrip()
    print(my_str1)

    结果:

    2、rstrip() :删除右侧空白字符

    实例:

    my_str = '   hello world and my and test and python   '  # 原始字符串
    print(my_str)
    # rstrip() 删除右侧空白字符
    my_str2 = my_str.rstrip()
    print(my_str2)

     结果:

    3、strip() 删除两侧空白字符

    实例:

    my_str = '   hello world and my and test and python   '    # 原始字符串
    print(my_str)
    # strip() 删除两侧空白字符
    my_str3 = my_str.strip()
    print(my_str3)

    结果:

     4、ljust() :字符串左对齐

    语法: 字符串序列.ljust('长度', 填充字符)

    实例:

    str_a = 'hello'
    new_str_a = str_a.ljust(10, '.')
    print(new_str_a)

    结果:

     5、rjust() :字符串右对齐

    语法: 字符串序列.rjust('长度', 填充字符)

    实例:

    str_a = 'hello'
    new_str_b = str_a.rjust(11, '+')
    print(new_str_b)

    结果:

    6、center() :字符串中间对齐

    语法: 字符串序列.center('长度', 填充字符)

    实例:

    str_a = 'hello'
    new_str_c = str_a.center(11, '*')
    print(new_str_c)

    结果:

  • 相关阅读:
    淘宝nginx的学习使用,安装及反向代理,负载均衡
    Linux5
    Linux4
    Linux权限相关及yum源的配置
    linux基本命令及python3的环境配置
    使用Guava RateLimiter限流
    Runnable与Callable 区别
    [Kafka] 如何保证消息不丢失
    [多线程] 等待所有任务执行完成
    [Docker] 快速安装mysql
  • 原文地址:https://www.cnblogs.com/zack-dong/p/14062795.html
Copyright © 2011-2022 走看看