前言
本章内容将学习字符串的提取,格式化字符串,大小写转换,访问剪贴板,复制粘贴文本等字符串操作。
1.转义字符
转义字符 | 打印为 |
\' | 单引号 |
\" | 双引号 |
\t | 制表符 |
\n | 换行符 |
\\ | 倒斜杠 |
要在python中输出单引号或者换行符等特殊字符作为字符内容,就要用到转义字符"\",格式是"\char",反斜杠加想添加到字符串中的字符。对于单引号字符,使用双引号包含来的更方便。
#1.转义字符
spam = 'Say hi to Bob\'s mother'
print(spam)
#双引号
spam = "That is Alice's cat"
print(spam)
2.原始字符
可以在字符串开始的引号之前加“r”,使其成为原始字符,“原始字符串”完全忽略所有的转义字符,打印出字符串中所有的反斜杠。
print(r'That is Carol\'s Dog')
3.三重引号的多行字符串
spam = '''Dear Alice
Eve's cat has bee arrested for catnapping, cat burglary, and extortion.
Sincerely,
Bob'''
print(spam)
结果:
python的代码块缩进规则不适用与多行字符串,在三重引号之间的所有的引号,制表符或换行,都被认为是字符串的一部分。这种方式更便于格式化输出。
4.字符串下标和切片
字符串详列表一样,使用下标和切片。可以将字符串"Hello world"看成是一个列表
spam = 'Hello world!'
print(spam[0])
print(spam[4])
print(spam[-1])
print(spam[0:5])
print(spam[:5])
print(spam[6:])
如果指定一个下标,得到的是字符串在该处的字符。
如果用一个下标和另一个下标指定一个范围,开始下标将被包含,结束下标则不被包含。
5.字符串的 in和not in操作符
使用类似列表,用in 和 not in 连接两个字符串得到的表达式,求值为布尔值True或False。
6.字符串方法
6.1 upper(),lower(),isupper(),islower()
upper()和lower()字符串方法返回一个新字符串,其中原字符串的所有字母都被相应的转换成大写或者小写,字符串中非字母数字保持不变。注意,两个操作方法并没有修改原来的字符串,而是返回一个转换后的字符串。
spam = 'Hello whrld!'
up_spam = spam.upper()
print('upper()->spam = '+ spam)
print('up_spam = ', up_spam)
lo_spam = spam.lower()
print('lower->spam = ' + spam )
print('lo_spam = ', lo_spam)
print('up_spam is upper', up_spam.isupper())
print('lo_spam is lower', lo_spam.islower())
如果需要进行与大小写无关的比较,upper和lower方法就很有用。
6.2 isX方法
除了大小写判断,还有几个字符换方法,他们的名字以is开头,返回一个布尔值,描述了字符串的特点。下面是一些常用的IsX字符串方法:
方法 | 说明 |
isalpha() | 如果字符串非空,且只包含字母,返回True |
isalnum() | 如果字符串非空,且只包含字母和数字,返回True |
isdecimal() | 如果字符串非空,且只包含数字字符,返回True |
isspace() | 如果字符串非空,且只包含空格,制表符和换行,返回True |
sititle() | 如果字符串非空,且只包含以大写字母开头,后面都是小写字母的单词 |
在交互式环境中使用上述方法
>>> 'hello'.isalpha()
True
>>> 'hello123'.isalpha()
False
>>> 'hello123'.isalnum()
True
>>> 'hello'.isalnum()
True
>>> '123'.isdecimal()
True
>>> ' '.isspace()
True
>>> 'This Is Title Case'.istitle()
True
>>> 'This Is Title Case 123'.istitle()
True
>>> 'This Is not Title Case 123'.istitle()
False
>>> 'This Is NOT Title Case 123'.istitle()
False
>>> 'This Is Not Title Case 123'.istitle()
True
使用实例:判断输入的字符是否符合要求
def collInfo():
while True:
print('Enter your age:')
age = input()
if age.isdecimal():
break
print('please enter anumber for your age.')
while True:
print('Enter a password for your count:')
passward = input()
if passward.isalnum():
break
print('passward can only have letters and numbers,try again!')
collInfo()
6.3 startswith()和endswith()
方法 | 说明 |
startswith() | 所调用的字符串以该方法传入的字符串开始 |
endswith() | 所调用的字符串以该方法传入的字符串结束 |
6.4 join()和split()
方法 | 说明 |
join() | 在一个字符串上调用,参数是一个字符串列表,返回一个字符串,返回的字符串由传入的列表中每个字符串连接而成。 |
split() | 所做的事情与join相反,这对一个字符串调用,返回一个字符串列表。 |
spam = ' li '#将要插入的字符
listspam = ['cat','rats','bats']
str1 = spam.join(listspam)
print(str1)
spam = 'My name is Simon'
listSpam = spam.split()
print(listspam)
print('type of listSpam is '+ str(type(listspam)) )
结果:
cat li rats li bats
['cat', 'rats', 'bats']
type of listSpam is <class 'list'>
注意,调用join()方法的字符串,被插入到列表参数中每个字符串的中间。要记住,join()方法是针对一个字符串来调用的,并且传如一个列表值(很容易使用其他方式调用)。split()所做的事情与join相反,这对一个字符串调用,返回一个字符串列表。
6.5 rjust()和ljust()和center()方法对齐文本
方法 | 说明 |
rjust(n,str) | 字符串右对齐,第一个参数是长度,第二个参数是长度不满足的时候,用于填充的字符,如果不指定第二个参数默认为空格 |
ljust() | 与右对齐对应,用法参数一致 |
center() | 与前两个方法相同,但是它让文本居中,两边填充字符 |
交互式环境中:
>>> 'Hello'.rjust(10)
' Hello'
>>> 'Hello whrld!'.rjust(10)
'Hello whrld!'
>>> 'Hello whrld!'.rjust(1)
'Hello whrld!'
>>> 'Hello whrld!'.rjust(20)
' Hello whrld!'
>>> 'Hello whrld!'.ljust(20)
'Hello whrld! '
>>> 'Hello whrld!'.ljust(20,'*')
'Hello whrld!********'
>>> 'Hello whrld!'.center(20,'*')
'****Hello whrld!****'
使用rjust(),ljust()和center可以让你却表字符串整齐对其,即使你不清楚到底有多少字符
6.6.用strip(),rstrip(),lstrip()删除空白字符
方法 | 说明 |
strip(str) | 返回一个新的字符串,开头或末尾都没有空白字符,可选参数传入一个字符串,指示变量中存储的字符串两端,删除 出现的字符中的字母,字符的顺序不重要,例如,strip('amPs'),删除出现的a,m,p和大写的S。 |
rstrip(str) | 返回一个新的自复仇,右边结尾处没有空白字符,可选参数传入一个字符串,指定最右边的那些字符应该删除, 注意,最右边如果是空白字符该入参没有用 |
lstrip(str) | 与rstrip()作用相同,对应的开头没有空白字符,可选参数传入一个字符串,指定右边的那些字符应该删除,同理, 传参时最左边不能是空白字符。 |
spam = ' Hello World!'
print(spam.rstrip('!'))
spam = 'XHello World'
print(spam.lstrip('X'))
spam = 'Hello World Aps'
print(spam.strip('psD'))
6.7 使用pyperclip()拷贝粘贴字符串
pyperclip 模块有 copy()和 paste()函数, 可以向计算机的剪贴板发送文本, 或从它接收文本。将程序的输出发送到剪贴板, 使它很容易粘贴到邮件、文字处理程序或其他软件中。
>>> import pyperclip
>>> pyperclip.copy('Hello world!')
>>> pyperclip.paste()
'Hello world!'
#在程序外拷贝一段代码在粘贴板中,此时粘贴板的内容已发生改变
>>> pyperclip.paste()
"spam = ' Hello World!'\r\nprint(spam.rstrip('!'))\r\nspam = 'XHello World'\r\nprint(spam.lstrip('X'))\r\nspam = 'Hello World Aps'\r\nprint(spam.strip('psD'))"
如果你的程序之外的某个程序改变了剪贴板的内容, paste()函数就会返回它。
使用实例:
将txt文件中每行字符前面都加上一个* 号,下面的程序演示从文件中读取内容,重新组装内容后再写回文件,其实用到的剪切板多此一举,就是为了使用。关于文件的打开与读写,本章不做介绍,后面学习到的时候再详细说明。
原字符内容如下:
List of animals
List of aquarium
List of biologists by auther abbreviation
List of cultivars
def DoLists():
import pyperclip#引用剪切板
#假设剪切板中的内容已复制
#这里使用python的文件操作,open,read,write,都是从网页上找的例子
print('now try to open the file named pystr1.txt')
tpath = './pystr1.txt'
fr = open(tpath,'r')#以读的方式打开文件
print('pystr1.txt opened success,now sart to read...')
text = fr.read()#读取所有字符
print(text)
pyperclip.copy(text)#这里多此一举是为了使用这个函数做例子
spam = pyperclip.paste()
lines = spam.split('\n')#返回列表
for i in range(len(lines)):
lines[i] = '*' + lines[i]
lines.append('* List of happy days')# lines = lines +['* List of happy days']
print(lines)
#将列表连接成字符串,加换行
text = '\n'.join(lines)
fw = open(tpath,'w')#以写的方式打开文本,原有内容会被删除,如果已经打开,从头开始编辑
fw.write(text)
fw.close()
DoLists()
运行结果:
文本文件已经被修改