• Python入门示例系列10 字符串(初级)


    Python入门示例系列10 字符串(初级)

    字符串(string)

    Python中的字符串用单引号 ' 或双引号 " 括起来,同时使用反斜杠 \ 转义特殊字符。

    s1="hello"
    s2='hello'
    s3=''
    s4=""
    s5='''hello'''
    s6="""hello"""
    s7=str()
    
    print(type(s1))
    print(type(s2))
    print(type(s3))
    print(type(s4))
    print(type(s5))
    print(type(s6))
    print(type(s7))

    结果:

    <class 'str'>
    <class 'str'>
    <class 'str'>
    <class 'str'>
    <class 'str'>
    <class 'str'>
    <class 'str'>

    字符串的索引(index)


    索引值以 0 为开始值,-1 为从末尾的开始位置。

     

     示例:

    str = 'ABCDEFG'
    print (str)          # 输出字符串
    print (str[0])       # 输出字符串第一个字符
    print (str[3]) 
    print (str[-1]) 
    print (str[-3]) 

    字符串的切片(slice)[:]


    字符串的截取子串的语法格式如下:

    变量[头下标:尾下标]   #注意,取头不取尾
    str = 'ABCDEFG'
    print (str)          # 输出字符串
    print (str[0:-1])    # 输出第一个到倒数第二个的所有字符
    print (str[2:5])     # 输出从第三个开始到第五个的字符
    print (str[2:])      # 输出从第三个开始的后的所有字符

    执行以上程序会输出如下结果:

    ABCDEFG
    ABCDEF
    CDE
    CDEFG

    字符串运算符

    假设 a="Hellow",b="Python"

    下表实例变量 a 值为字符串 "Hello",b 变量值为 "Python":

    操作符描述实例
    + 字符串连接
    >>>a + b
    'HelloPython'
    * 重复输出字符串
    >>>a * 2
    'HelloHello'
    [  ] 通过索引获取字符串中字符
    >>>a[1]
    'e'
    [ : ] 截取字符串中的一部分
    >>>a[1:4]
    'ell'
    in 成员运算符 - 如果字符串中包含给定的字符返回 True
    >>>"H" in a
    True
    not in 成员运算符 - 如果字符串中不包含给定的字符返回 True
    >>>"M" not in a
    True
    r/R 原始字符串 - 原始字符串:所有的字符串都是直接按照字面的意思来使用,没有转义特殊或不能打印的字符。 原始字符串除在字符串的第一个引号前加上字母"r"(可以大小写)以外,与普通字符串有着几乎完全相同的语法。
    >>>print(r'\n')
    \n
    >>> print(R'\n')
    \n

     
    加号 + 是字符串的连接符,把两个字符串连接起来(拼接起来)。

    星号 * 表示复制当前字符串,与之结合的数字为复制的次数(重复多次,复制多次)。

    实例如下:

    str = 'ABCDEFG'
    print (str)          # 输出字符串
    print (str * 2)      # 输出字符串两次,也可以写成 print (2 * str)
    print (str + "TEST") # 连接字符串

    执行以上程序会输出如下结果:

    ABCDEFG
    ABCDEFGABCDEFG
    ABCDEFGTEST

    转义字符


    Python 使用反斜杠 \ 转义特殊字符,如果你不想让反斜杠发生转义,可以在字符串前面添加一个 r,表示原始字符串(r 指 raw,即 raw string,会自动将反斜杠转义):
    实例

    >>> print('AB\nCD')
    AB
    CD
    >>> print(r'AB\nCD')
    AB\nCD

    常见转义字符

    \\ 反斜杠符号 Backslash
    >>> print("\\")
    \
    \' 单引号 Single quote
    >>> print('\'')
    '
    \" 双引号 Double quote
    >>> print("\"")
    "
    \n 换行 Linefeed
    >>> print("\n")
    
    
    >>>
    \t 横向制表符 Horizontal Tab
    >>> print("Hello \t World!")
    Hello      World!
    >>>
    \r 回车,将 \r 后面的内容移到字符串开头,并逐一替换开头部分的字符,直至将 \r 后面的内容完全替换完成。Carriage Return
    >>> print("Hello\rWorld!")
    World!
    >>> print('google taobao\r123456')
    123456 taobao
    \ooo 八进制数,o 代表 0~7 的字符,例如:\012 代表换行。octal value
    >>> print("\110\145\154\154\157")
    Hello
    \xyy 十六进制数,以 \x 开头,y 代表的字符,例如:\x0a 代表换行. hex value
    >>> print("\x48\x65\x6c\x6c\x6f")
    Hello



    注意,Python 没有单独的字符类型,一个字符就是长度为1的字符串。

    >>> word = 'Python'
    >>> print(word[0], word[5])
    P n
    >>> print(word[-1], word[-6])
    n P


    与 C 字符串不同的是,Python 字符串不能被改变。向一个索引位置赋值,比如 word[0] = 'm' 会导致错误。


    字符串内建函数

    字符串有很多内建函数(https://www.cnblogs.com/emanlee/p/3616755.html),下面列出几个常用的函数。

    string.format()
      格式化字符串 (https://www.cnblogs.com/emanlee/p/15816634.html)
    string.lower()
      转换 string 中所有大写字符为小写.
    string.upper()
      转换 string 中的小写字母为大写
    string.replace(old, new,  num=string.count(str1))
      把 string 中的 old 替换成 new, 如果 num 指定,则替换不超过 num 次.

     示例:

    >>> "abc".upper()
    'ABC'
    >>> "ABC".lower()
    'abc'
    >>> "ABCDEF".replace("AB","12345")
    '12345CDEF'
    >>> 

    系列目录

    Python入门示例系列01 为什么学Python

    Python入门示例系列02 Python 语言的特点

    Python入门示例系列03 安装Python开发工具

    Python入门示例系列04 使用 IDLE Shell

    Python入门示例系列05 使用PyCharm

    Python入门示例系列06 使用PyCharm单步调试

    Python入门示例系列07 Python注释

    Python入门示例系列08 基础语法

    Python入门示例系列09 Python算术运算

    Python入门示例系列10 字符串(初级)

    Python入门示例系列11 数据类型

    Python入门示例系列12 数据类型转换

    REF

    https://www.cnblogs.com/emanlee/p/3616755.html

    https://www.runoob.com/python3/python3-data-type.html

    https://www.runoob.com/python3/python3-string.html

    https://www.cnblogs.com/mysterious-killer/p/10033830.html

  • 相关阅读:
    php遍历目录
    PHP处理一个5G文件,使用内存512M的,数据为整形,从大到小排序,优化排序算法
    百钱买百鸡问题 php版本
    青蛙跳100级台阶算法,完整可运行,php版本
    网站如何整体换角度
    SOA架构设计(转发)
    一些稍微复杂点的sql语句
    php变量和数组大小限制
    uploadify中文开发文档,解决php多图上传
    mysql索引的一些知识
  • 原文地址:https://www.cnblogs.com/emanlee/p/15815353.html
Copyright © 2020-2023  润新知