Python字符串操作

字符串字面量

双引号

1
2
3
>>> spam = "This is Tom' cat"
>>> spam
"This is Tom' cat"

转义字符

1
2
3
>>> print('\',\",\t,\n,\\')
',", ,
,\

原始字符串

在引号之前加上r,使他成为原始字

1
2
>>> print(r'That is Carol\' cat')
That is Carol\' cat

用三重引号的多行字符串

1
2
3
4
5
6
7
8
>>> print('''Dear Tom:
Thank for your help
Tjanks;
Lili''')
Dear Tom:
Thank for your help
Tjanks;
Lili

字符串下标和切片

1
2
3
4
5
6
7
8
9
10
11
12
13
>>> spam = 'hello world!'
>>> spam[0]
'h'
>>> spam[4]
'o'
>>> spam[-1]
'!'
>>> spam[0:5]
'hello'
>>> spam[:5]
'hello'
>>> spam[6:]
'world!'

字符串中的in 和not in 操作符

1
2
>>> 'hello' in 'hello word!'
True

有用的字符串方法

字符串方法 upper(), lower(), isupper(), islower()

1
2
3
4
5
6
7
8
9
10
>>> spam = 'Hello word!'
>>> spam.upper() #将字符串中的字母转换为大写
'HELLO WORD!'
>>> spam = spam.lower() #将字符串中的字母转换为小写
>>> spam
'hello word!'
>>> 'Hello'.islower() #判断字符串中字符是否都为小写,自动忽略非字母的部分
False
>>> 'Hello'.isupper() #判断字符串中字符是否全为大写,自动忽略非字母的部分
False

isX字符串方法

  • isalpha() 返回Ture,字符串只包含字母,并且非空
1
2
3
4
5
6
>>> 'hello'.isalpha()
True
>>> 'hello '.isalpha()
False
>>> 'hello1223'.isalpha()
False
  • isalnum() 返回Ture,字符串只包含数字和字母,并且非空
1
2
3
4
5
6
>>> 'hello'.isalnum()
True
>>> 'hello '.isalnum()
False
>>> 'h545'.isalnum()
True
  • isdecimal() 返回True,字符串只包含数字,并且非空
1
2
3
4
>>> '123'.isdecimal()
True
>>> 'hello123'.isdecimal()
False
  • isspace 返回True,字符串只包含空格、制表符、换行并且非空
1
2
>>> ' '.isspace()
True
  • istitle() 返回True,字符串仅以大写字母开头
1
2
3
4
5
6
7
8
>>> 'This Is Title Case'.istitle()
True
>>> 'This Is Title Case 123'.istitle()
True
>>> 'This Is not Title Case'.istitle()
False
>>> 'This Is NOT Title Case'.istitle()
False

字符串方法 startswith() 和 endswith()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
>>> 'hello word!'.startswith('Hello')
False
>>> 'hello word!'.startswith('hello')
True
>>> 'hello word!'.startswith('he')
True
>>> 'hello word!'.startswith('h')
True
>>> 'hello word!'.endswith('!')
True
>>> 'hello word!'.endswith('word!')
True
>>> 'hello word!'.endswith('d')
False

字符串方法join() 和split()

1
2
3
4
>>> '--'.join(['cats','rats','bats'])  #组合字符串
'cats--rats--bats'
>>> 'My nmae is Simon'.split() #拆分字符串
['My', 'nmae', 'is', 'Simon']

用rjust()、ljust()、center() 方法对齐

1
2
3
4
5
6
7
8
9
10
11
12
13
>>> 'Hello'.rjust(10)
' Hello'
>>> 'Hello'.rjust(20)
' Hello'
>>> 'Hello'.ljust(10)
'Hello '
>>> 'Hello '.center(10)
' Hello '
>>> 'Hello'.center(5)
'Hello'
#当字符不够时才不填充
>>> 'Hello'.center(4)
'Hello'

默认填充为空格

1
2
3
4
5
6
>>> 'Hello'.rjust(20, '*')
'***************Hello'
>>> 'Hello'.ljust(10, '-')
'Hello-----'
>>> 'Hello'.center(10, '+')
'++Hello+++'

用strip()、rstrip()、lstrip() 删除空白字符

1
2
3
4
5
6
>>> ' hello word '.strip()
'hello word'
>>> ' hello word'.lstrip()
'hello word'
>>> 'hello word '.rstrip()
'hello word'

strip() 括号中的字母顺序不影响结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
>>> spam = 'SpamSpamBaconSpamEggsSpamSpam'
>>> spam.rsplit()
['SpamSpamBaconSpamEggsSpamSpam']
>>> spam.rstrip('Spam')
'SpamSpamBaconSpamEggs'
>>> spam = 'SpamSpamBaconSpamEggsSpamSpam'
>>> spam.rstrip('mapS')
'SpamSpamBaconSpamEggs'
>>> spam = 'SpamSpamBaconSpamEggsSpamSpam'
>>> spam.rstrip('mpaS')
'SpamSpamBaconSpamEggs'
>>> spam = 'SpamSpamBaconSpamEggsSpamSpam'
>>> spam.strip('Spam')
'BaconSpamEggs'

用pyperclip模块拷贝粘贴字符串

pyperclip()模块有copy()、paste() 两个函数,可以向计算机的剪贴板发送文本、或者从它接收文本

1
2
3
4
>>> import pyperclip
>>> pyperclip.copy('Hello')
>>> pyperclip.paste()
'Hello'

小项目:口令保险箱

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#! python3
# pw.py - An insecure password locker program.

PASSWORDS = {'email':'test@test.com',
'blog':'www.appblog.cn',
'luggage':'123456' }
import sys,pyperclip
if len(sys.argv)<2:
print('Usage:python pw.py [account] - copy account password')
sys.exit()

account = sys.argv[1] #这里应该是account name

if account in PASSWORDS:
pyperclip.copy(PASSWORDS[account])
print('Password for ' + account + ' is copied to clipboard.')
else:
print('There is no account named '+ account)

小项目:在Wiki标记中添加无序列表

1
2
3
4
5
6
7
8
9
10
11
12
#! python3
# bulletPointAdder.py

import pyperclip
text = pyperclip.paste()

temp = text.split('\n')
for i in range(len(temp)):
temp[i] = '* ' + temp[i]

text = '\n'.join(temp)
pyperclip.copy(text)

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :