1. 字符串的格式化
字符串的格式化通常用在print()函数中,用来实现输出字符串的特点样式。格式说明符和普通字符构成一个格式字符串,通过格式云算符%限定输出数据的显示样式。
格式字符串的格式如下:
<格式字符串>%(<值1>,<值2>,...<值n>)
>>> print('今天是%d年%d月%d日,天气%s!'%(2019,10,24,'晴'))
今天是2019年10月24日,天气晴!
常用的格式说明符
符号 | 描述 |
---|---|
%c | 字符及其ASCII码 |
%s | 字符串 |
%d | 十进制数 |
%o | 八进制数 |
%x | 十六进制整数(用小写字母) |
%X | 十六进制整数(用大写字母) |
%f | 浮点数字,可指定小数点后的精度 |
%e | 浮点数字,科学计数法,用小写e |
%E | 浮点数字,科学计数法,用大写E |
%g或%G | 浮点数字,根据值采用不同模式 |
>>> print('%c' % 97)
a
>>> print('%c %c %c' % (97, 98, 99))
a b c
>>> print('%o' % 10)
12
>>> print('%x' % 10)
a
>>> print('%X' % 10)
A
>>> print('%f' % 27.658)
27.658000
>>> print('%e' % 27.658)
2.765800e+01
>>> print('%E' % 27.658)
2.765800E+01
- 字符串的索引与分片
①
索引
字符串的字符按位置进行了编号,称为索引,使用时可以通过这个编号访问字符串中的特定字符。字符串的第一个字符编号为0,一个长度为L的字符串的最后一个字符编号为L-1。例如,可以通过以下方式访问指定字符:
>>> str='God wants to check the air quality'
>>> str[0],str[1],str[18]
('G', 'o', ' ')
python同时允许根据索引反向访问字符串,此时字符串的编号从-1开始。例如:
>>> str='God wants to check the air quality'
>>> str[-1],str[-13],str[-26]
('y', 'e', 's')
②分片
字符串的分片指通过索引对字符串进行切片操作。分片操作格式如下:
<字符串名>[i:j:k]
i 表示起始编号,j 表示结束编号,k 表示编号增加步长。
[注]:切片的位置不包含 j 位置上的字符。例如:
>>> str='God wants to check the air quality'
>>> str[0:8:2]
'Gdwn'
分片语句中,i j k 均可以省略。i 省略时,表示从0或-1开始;j 省略时,表示到最后一个字符;k 省略时,表示步长为1。例如:
>>> str='God wants to check the air quality'
>>> str[0:8:2]
'Gdwn'
>>> str='God wants to check the air quality'
>>> str[4:18]
'wants to check'
>>> str[:2]
'Go'
>>> str[23::]
'air quality'
利用字符分片操作,逆序输出字符串:
>>> str='1234567'
>>> str[-1::-1]
'7654321'
- 字符串的基本运算
字符串基本运算符及功能
运算符 | 功能 |
---|---|
s1+s2 | 连接字符串 s1 和 s2 |
s1*n | 生成n个s1组成的字符串 |
s1 in s2 | 如果 s1 是 s2 的子串,返回True,否则返回False |
>,<,== | 比较字符串的ASCII码,多个字符时从左向右一次比较 |
字符串运算实例
>>> s1='Python 程序入门'
>>> s2='设计与实践'
>>> s1+s2
'Python 程序入门设计与实践'
>>> n=2
>>> s1*n
'Python 程序入门Python 程序入门'
>>> s1 in s2
False
>>> 'a'>'A' #单个字符的比较ASCII码的值,a为97,A为65
True
>>> 'this is a test'>'this is' #多个字符的比较ASCII码的值,从左向右依次比较每个字符
True
- 字符串运算函数
函数名 | 功能 |
---|---|
len(s) | 返回集合长度 |
chr(x) | 返回整数 x 对应的ASCCII字符 |
ord(s) | 返回一个字符的ASCII码 |
str(x) | 将数字转化为字符串 |
>>> len('Python 程序入门设计与实践')
16
>>> print(ord('a'),ord('A'))
97 65
>>> print(chr(66),chr(98))
B b
>>> '10'+str(3.1415)
5.字符串运算方法
调用字符串方法的格式如下:
<字符串名>.<方法名>(<参数>)
字符串常用方法:
方法名 | 功能 |
---|---|
s.lower() | 将字符串转换为小写字母 |
s.upper() | 将字符串转换为大写字母 |
s.capitalize() | 将字符串转换为首字母大写 |
s.title() | 将第一个字符转换为大写 |
s.replace(old,new,[count]) | 将s中的old字符串替换为new,count为替换次数 |
s.spilit(sep,[maxspilit]) | 以sep为分隔符,将s拆分为一个列表,默认分隔符为空格,maxspikit表示拆分的次数,默认为-1,表示无限制 |
s.find(s1,[start],[end]) | 返回s1在s中出现的位置。如果没有则返回-1。 |
s.count(s1,[start],[enf]) | 返回s1在s中出现的次数 |
s.isalumn() | 判断s是否为全字母和数字,且至少一个字符 |
s.isalpha() | 判断s是否为全字母,且至少一个字符 |
s.isupper() | 判断s是否为全大写字母 |
s.islower() | 判断s是否为全小写字母 |
s.format() | 格式输出字符(用法与格式运算符%类似) |
字符串大小写转换方法实例:
>>> s.upper()
'THIS IS PYTHON'
>>> s.capitalize()
'This is python'
>>> s.title()
'This Is Python'
字符串拆分方法实例:
>>> s='this is python'
>>> s.split()
['this', 'is', 'python']
>>> s.split(sep=' ',maxsplit=1)
['this', 'is python']
字符串替换和查找方法的实例:
>>> s='this is python'
>>> s.find('t')
0
>>> s.count('t')
2
>>> s.replace('t',"T",2)
'This is pyThon'
字符串格式化方法的实例:
>>> a='this is %s %s'%("an","apple")
>>> a
'this is an apple'
>>> b='this is {} {}'.format('apple','an')
>>> b
'this is apple an'
>>> c='this is {1} {0}'.format('apple','an')
>>> c
'this is an apple'
>>> d='this is {number} {fruit}'.format(number='an',fruit='apple')
>>> d
'this is an apple'
统计文章中出现单词的次数:
# author:answer time:2019/10/23
passge='Do not trouble trouble till trouble toubles you'
word='trouble'
n=passge.count(word)
print('%s出现的次数为:%s'%(word,n))
trouble出现的次数为:3