• 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))
    • 运行结果:

  • 相关阅读:
    Android listview 的应用
    Android 创建自定义布局
    Android Dialog
    android ProgressBar
    Lilac Pwn stack4-stack_pivoting Writeup
    CTFHUB Pwn ret2shellcode Writeup
    Lilac Pwn stack3-rop Writeup
    函数调用过程与栈帧结构
    线性回归及Python实现
    Google Kick Start Round A 2020
  • 原文地址:https://www.cnblogs.com/lmei/p/5307117.html
Copyright © 2020-2023  润新知