zoukankan      html  css  js  c++  java
  • python--基础学习(四)自然字符串、重复字符串、子字符串

    python系列均基于python3.4环境

    1、自然字符串和重复字符串

    • 代码示例:
    str1=r'hello 
    python' 
    str2='hello 
    python'
    str3="hello python
    "*3
    
    print("str1: {0}".format(str1))
    print("str2: {0}".format(str2))
    print("str3: {0}".format(str3))
    • 运行结果:

    • 结果分析:

    (1)str1为自然字符串,输出结果保留原来的格式,不受转义影响

    (2)str2为非自然字符串,输出结果受转义影响

    (3)str3为原字符串重复3次

    2、子字符串

    (1)索引从0开始

    (2)切片运算符[x:y]表示:x<=(下标)<y

    • 代码示例:
    str="hello,python"
    
    substring1=str[0]    #索引为0的字符
    substring2=str[6]    #索引为6的字符
    substring3=str[:5]   #截取索引从0到(5-1)的字符
    substring4=str[6:]   #截取索引从6到结束的字符
    substring5=str[6:8]  #截取索引从6到(8-1)的字符
    
    print("substring1: {0}".format(substring1))
    print("substring2: {0}".format(substring2))
    print("substring3: {0}".format(substring3))
    print("substring4: {0}".format(substring4))
    print("substring5: {0}".format(substring5))
    • 运行结果:

  • 相关阅读:
    Rolling File Appender使用示例
    log4net生成dll文件
    看涨期权(call options)
    log4net file Appender使用示例
    log4net不能记录日志,IsErrorEnabled值为false
    C#委托
    打印事件处理顺序
    Zigbee、WiFi和433MHz无线技术
    log4net Tutorial
    安装服务出现异常
  • 原文地址:https://www.cnblogs.com/lmei/p/5307117.html
Copyright © 2011-2022 走看看