zoukankan      html  css  js  c++  java
  • python字符串

     

     

    # isspace方法判断空白字符,转义字符
    space_str="    "
    print(space_str.isspace())
    
    space_str1="    	
    
    "
    print(space_str1.isspace())
    
    # 判断字符串是否只包含数字
    # 下面三个方法都不能判断小数
    # 三个方法判断的数字范围不同
    num_str="5"
    print(num_str.isdecimal())
    print(num_str.isdigit())
    print(num_str.isnumeric())
    
    # 查找和替换
    hello_str="hello python"
    print(hello_str)
    
    # 是否以指定字符串开始
    print(hello_str.startswith("he"))
    
    # 是否以指定字符串结尾
    print(hello_str.endswith("on"))
    
    # 查找指定字符串
    # index方法同样可以查找指定字符串的位置
    # index方法若指定的字符串的位置不存在,会报错
    # 方法若指定的字符串的位置不存在,会输出-1
    print(hello_str.find("llo"))
    
    # 替换字符串
    # replace方法执行完成后,会返回一个新的字符串
    # 注意:并不会修改原来字符串
    print(hello_str.replace("python","world"))
    print(hello_str)
    #文本对齐
    # 把下列内容顺序并居中
    poem=["登鹳雀楼",
          "王之涣",
          "白日依山尽",
          "黄河入海流",
          "欲穷千里目",
          "更上一层楼"]
    
    for poen_str in poem:
        print("|%s|" %poen_str.center(10," "))
    #去除空白字符
    poem=["	
    登鹳雀楼",
          "王之涣",
          "白日依山尽	
    ",
          "黄河入海流",
          "欲穷千里目",
          "更上一层楼"]
    
    for poen_str in poem:
        # 先用strip方法去除空白字符
        # 再使用center方法居中
        print("|%s|" %poen_str.strip().center(10," "))
    #拆分和连接
    # 将字符串中的空白字符去掉
    # 再使用" "作为分隔符,拼接成一个完整的字符串
    poem_str="登鹳雀楼	王之涣	白日依山尽	
    黄河入海流	欲穷千里目
    更上一层楼"
    
    print(poem_str)
    
    poem_list=poem_str.split()
    print(poem_list)
    
    # 合并字符串
    result="  ".join(poem_list)
    print(result)

     

    #字符串的切片
    num_str="123456789"
    # 截取从2到5的字符串
    print(num_str[2:6])
    # 截取从2- 末尾的字符串
    print(num_str[2:])
    # 截取开始~5位置的字符串
    print(num_str[:6])
    # 截取完整的字符串
    print(num_str[:])
    # 从开始位置,每隔一个字符截取字符串
    print(num_str[0::2])
    # 从索引1开始,每隔一个取一个
    print(num_str[1::2])
    # 截取2~末尾-1的字符
    print(num_str[2:-1])
    # 截取字符串末尾两个字符
    print(num_str[-2:])
    # 字符串的逆序
    print(num_str[::-1])
  • 相关阅读:
    SQL优化
    Mybatis
    Spring MVC(总结二)
    ES多机集群配置
    ES索引,分片,一致性
    ElasticSearch关于索引库的命令操作
    试题01(一)
    Linux安装配置elastic search
    Windows安装配置elastic search
    SpringBoot(二)
  • 原文地址:https://www.cnblogs.com/wang-xun/p/11395324.html
Copyright © 2011-2022 走看看