zoukankan      html  css  js  c++  java
  • Python学习笔记(二) 字符串

    字符串属于序列的一种,这篇文章主要讲解字符串的常用方法以及格式设置

    一、通用序列操作

    1、创建字符串

    Python 允许我们使用单引号 '' 或双引号 “” 创建字符串,只要左右两边的引号保持一致就行

    >>> a = 'Hello Wrold'
    >>> b = "Hello Python"
    

    当然我们也可以使用内置函数 str() 来创建字符串或将其他序列转化为字符串

    >>> li = ['I','Love','Python']
    >>> c = str(li)
    >>> c
    # "['I', 'Love', 'Python']"
    >>> # 这似乎不太符合我们的预期,或许我们更希望将序列中的每一个元素合并成一个新的字符串
    >>> # 为此,我们可以使用内置方法 join 达到这种效果
    >>> d = ' '.join(li)
    >>> d
    # 'I Love Python'
    

    另外,这里再介绍几种在实际情况中经常会使用到的字符串类型

    (1)转义字符:在有特殊含义的字符前加上 ,例如 表示换行, 表示缩进,可以使用 本身进行转义

    >>> string = 'Hello
    My	friend'
    >>> print(string)
    # Hello
    # My	friend
    

    (2)原始字符串:在字符串引号前加上 r,用于表示不对字符串内容进行转义

    >>> string = r'Hello
    My	friend'
    >>> print(string)
    # Hello
    My	friend
    

    (3)长字符串:用三引号代替普通的引号,在换行处会自动加上 ,用于表示篇幅较长的文段

    >>> string = '''To see a world in a grain of sand,
    And a heaven in a wild flower,
    Hold infinity in the palm of your hand,
    And eternity in an hour.'''
    >>> print(string)
    # To see a world in a grain of sand,
    # And a heaven in a wild flower,
    # Hold infinity in the palm of your hand,
    # And eternity in an hour.
    

    2、索引与切片

    与列表相似,str 类型可以使用 [] 对其中某一个元素进行索引或者对其中某一段元素进行切片

    >>> string = 'Never give up, Never lose hope.'
    >>> string[13]
    # ','
    >>> string[:13]
    # 'Never give up'
    

    注意:切片得到的字符串是原字符串的一份副本,而非原字符串本身,所以切片操作并不会影响原字符串

    3、字符串拼接

    我们可以使用 + 拼接不同的字符串

    >>> string = 'D:\'
    >>> string += 'Document'
    >>> string
    # 'D:\Document'
    

    字符串其实是 不可变序列,一旦创建就不可被修改,但是上面对字符串进行拼接又该怎么理解呢?

    请大家再看看下面的例子

    >>> string = '123'
    >>> id(string)
    58561952
    >>> string += '456'
    >>> id(string)
    58561888
    

    可以看到,两次输出变量 string 的 id 是不一样的

    也就是说执行 string += '456' 时,实际上是新建立了一个变量 string,它的值为原来的 '123' 加上 '456'

    二、字符串方法

    由于字符串是 Python 中最为常用的变量类型之一,所以 Python 中内置了许多关于字符串的方法

    下面我们将会一一进行讲解,建议大家最好可以亲自动手敲一遍,理解各个方法的使用场景以及限制

    不过,就算记不住所有的用法也是正常的,毕竟字符串的方法多而繁杂,在实际使用的时候查查手册就好

    • capitalize():将字符串的第一个字符转换为大写字母

    • title():将所有单词的第一个字符转换为大写字母

    • upper():将字符串的所有字符转化为大写字母

    • lower():将字符串的所有字符转化为小写字母

    • swapcase():将字符串的所有字符的大小写互换

    >>> string = 'while there is life there is hope.'
    >>> string.capitalize() # 将字符串的第一个字符转换为大写字母
    # 'While there is life there is hope.'
    >>> string.title() # 将所有单词的第一个字符转换为大写字母
    # 'While There Is Life There Is Hope.'
    >>> string.upper() # 将字符串的所有字符转化为大写字母
    # 'WHILE THERE IS LIFE THERE IS HOPE.'
    >>> string = 'AaBbCc'
    >>> string.swapcase() # 将字符串的所有字符的大小写互换
    # 'aAbBcC'
    
    • center(width[, fillchar]):将字符串居中,并用 fillchar(默认为空格)填充至 width

    • ljust(width[, fillchar]):将字符串靠左,并用 fillchar(默认为空格)填充至 width

    • rjust(width[, fillchar]):将字符串靠右,并用 fillchar(默认为空格)填充至 width

    注意:如果 width 小于字符串宽度则直接返回字符串,不会截断输出

    >>> title = 'Title Here'
    >>> print(title.center(50,'*'))
    # ********************Title Here********************
    >>> lpara = 'Para Start Here'
    >>> print(lpara.ljust(50,'-'))
    # Para Start Here-----------------------------------
    >>> rpara = 'Para End Here'
    >>> print(rpara.rjust(50,'-'))
    # -------------------------------------Para End Here
    
    • strip([char]):去掉字符串前后的 char 字符(默认为空格或换行)

    • lstrip([char]):去掉字符串前的 char 字符(默认为空格或换行)

    • rstrip([char]):去掉字符串后的 char 字符(默认为空格或换行)

    注意:该方法只能处理字符串开头或结尾的字符,不能用于处理中间部分的字符

    >>> string = '000000100010'
    >>> string.strip('0')
    # '10001
    >>> string.lstrip('0')
    # '100010'
    >>> string.rstrip('0')
    # '00000010001'
    
    • split([sep [,maxsplit]]):按照 sep(默认空格或换行)对字符串进行切片,最大分隔次数为 maxsplit

    • splitlines([keepends]):按照换行符(' ', ' ', ')对字符串进行切片

      若 keepends 为 False (默认),则切片中不保留换行符,反之,则保留换行符

    • partition(sep):从左到右寻找第一次出现的 sep 对字符串进行分隔,返回一个三元元组

      第一个为分隔符左边的子串,第二个为分隔符本身,第三个为分隔符右边的子串

    • rpartition(sep):从右到左寻找第一次出现的 sep 对字符串进行分隔,返回一个三元元组

      第一个为分隔符左边的子串,第二个为分隔符本身,第三个为分隔符右边的子串

    • join(sequence):将字符串插入到 sequence 字符串每两两相邻的字符中间

    >>> string = '''Forget about the days when it's been cloudy.
    But don't forget your hours in the sun.'''
    >>> string.split()
    # ['Forget', 'about', 'the', 'days', 'when', "it's", 'been', 'cloudy.',
    # 'But', "don't", 'forget', 'your', 'hours', 'in', 'the', 'sun.']
    >>> string.splitlines()
    # ["Forget about the days when it's been cloudy.", "But don't forget your 
    # hours in the sun."]
    >>> string.partition('the')
    # ('Forget about ', 'the', " days when it's been cloudy.
    But don't forget
    # your hours in the sun.")
    >>> string.rpartition('the')
    # ("Forget about the days when it's been cloudy.
    But don't forget your
    # hours in ", 'the', ' sun.')
    >>> string = 'LOVE'
    >>> string.join('python')
    # 'pLOVEyLOVEtLOVEhLOVEoLOVEn'
    
    • find(sub [,start [,end]]):从左到右检查 sub 是否包含在字符串从 start 到 end 范围中

      返回第一个符合要求的子串的索引值,若没有找到则返回 -1

    • rfind(sub [,start [,end]]):从右到左检查 sub 是否包含在字符串从 start 到 end 范围中

      返回第一个符合要求的子串的索引值,若没有找到则返回 -1

    >>> string = 'abcacba'
    >>> string.find('a')
    # 0
    >>> string.rfind('a')
    # 6
    
    • count(sub [,start [, end]]):寻找字符串中从 start 到 end 之间 sub 出现的次数
    >>> string = 'Hello World'
    >>> string.count('o')
    # 2
    
    • beginswith(sub [,start [,end]]):检查字符串从 start 到 end 范围中的子串是否以 sub 子串开始

    • endswith(sub [,start [,end]]):检查字符串从 start 到 end 范围中的子串是否以 sub 子串结束

    >>> string = 'Whatever happens,happens for a reason.'
    >>> string.startswith('What')
    # True
    >>> string.endswith('nosaer')
    # False
    
    • replace(old, new[, max]):将原字符串中 old 子串转换为 new 子串,最大替换次数为 max
    >>> string = 'If you have an apple and I have an apple,and we exchange apples,we each now have two apples.'
    >>> string.replace('apple','idea')
    # 'If you have an idea and I have an idea,and we exchange ideas,we each now have two ideas.'
    
    • translate(table):根据 table 制定的规则替换字符
    >>> string = 'This is an example'
    >>> table = str.maketrans('aeiou','12345')
    >>> string.translate(table)
    'Th3s 3s 1n 2x1mpl2'
    

    三、格式设置

    对于字符串的格式设置,在 Python 早期的解决方案中,主要使用 字符串格式设置运算符

    这种方法类似于 C 语言中经典函数 printf 的用法

    在格式字符串中使用转换说明符表示待插入值的位置、类型和格式,在格式字符串后写出待插入的值

    >>> string = 'Hello, %s' % 'Jude'
    >>> string
    # 'Hello, Jude'
    

    上述格式字符串中的 %s 称为转换说明符,其中 s 表示待插入的值是字符串类型的变量,常用的格式说明符有:

    转换说明符 说明
    %c 字符
    %d 使用十进制数表示整数
    %e 使用科学记数法表示小数
    %f 使用定点表示法表示小数
    %g 根据数的大小确定使用科学记数法或定点表示法
    %s 字符串

    后来,Python 采用了一种新的解决方案,那就是字符串方法 format()

    在格式字符串中用花括号将替换字段括起表示待插入值的位置、索引名称和格式,在格式字符串后写出待插入的值

    # 若在 {} 中没有用于索引的名称,则默认使用位置参数
    >>> string = 'Hello, {} and {}'.format('Alan','Brian')
    >>> string
    # 'Hello, Alan and Brian'
    
    # 其实,位置参数无需按顺序进行排列
    >>> string = '{3} {0} {2} {1} {3} {0}'.format('be','not','or','to')
    >>> string
    # 'to be or not to be'
    
    # 当然,索引名称还可以使用关键字参数
    >>> string = '{name} is {age} years old'.format(name='Helen',age=18)
    >>> string
    # 'Helen is 18 years old'
    

    注意:如果指定的值不是字符串,则将默认使用 str() 内置方法将其转化为字符串

    我们可以在花括号内使用 转换标志格式说明符 指定插入字段的格式

    • 转换标志:跟在 感叹号 后的单个字符,表示使用对应的格式转换给定值
    转换标志 含义
    r 表示 repr,创建给定值的 Python 表示
    s 表示 str,创建给定值的普通字符串版本
    a 表示 ascii,创建给定值的 ASCII 字符表示
    >>> print('{pi!r}
    {pi!s}
    {pi!a}'.format(pi='π'))
    # 'π'
    # π
    # 'u03c0'
    
    • 格式说明符:跟在 冒号 后的表达式,用于详细指定字符串的格式,包括数的类型、宽度和精度、对齐和填充

    指定数的类型

    类型说明符 说明
    d 将整数表示为十进制数(这是整数默认使用的说明符)
    b 将整数表示为二进制数
    o 将整数表示为八进制数
    x 将整数表示为十六进制数
    g 自动在定点表示法和科学记数法之间做出选择(这是小数默认使用的说明符)
    e 将小数表示为科学记数法
    f 将小数表示为定点数
    s 保持字符串的格式不变(这是字符串默认使用的说明符)
    c 将字符表示为 Unicode 码点
    >>> print('Integer
    in decimal: {0:d}
    in binary: {0:b}'.format(10))
    # Integer
    # in decimal: 10
    # in binary: 1010
    >>> print('Fraction
    in fixed-point notation: {0:f}
    in scientific notation: {0:e}'.format(0.25))
    Fraction
    in fixed-point notation: 0.250000
    in scientific notation: 2.500000e-01
    

    指定宽度(默认右对齐)

    >>> print("{0:10}".format(3.14))
    #       3.14
    

    指定精度

    >>> print("{0:.2f}".format(10/3))
    # 3.33
    

    同时指定宽度和精度

    >>> print("{0:10.2f}".format(1/4))
    #       0.25
    

    指定左对齐,居中和右对齐,填充符号默认为空格

    >>> print("{0:<10.2f}
    {0:^10.2f}
    {0:>10.2f}".format(3.14))
    # 3.14      
    #    3.14   
    #       3.14
    >>> print("{0:*<10.2f}
    {0:*^10.2f}
    {0:*>10.2f}".format(3.14))
    # 3.14******
    # ***3.14***
    # ******3.14
    

    【 阅读更多 Python 系列文章,请看 Python学习笔记

    版权声明:本博客属于个人维护博客,未经博主允许不得转载其中文章。
  • 相关阅读:
    PHP curl_setopt函数用法介绍补充篇
    Javascript的setTimeOut和setInterval的定时器用法
    PHP curl_setopt函数用法介绍上篇
    开启PHP的伪静态
    关于MySQL的几个命令之load
    使用PHP生成和获取XML格式数据
    WEB开发中常用的正则表达式
    WEB开发中的页面跳转方法总结
    PHP的serialize序列化数据与JSON格式化数据
    PHP防止重复提交表单
  • 原文地址:https://www.cnblogs.com/wsmrzx/p/9980399.html
Copyright © 2011-2022 走看看