Python字符串操作

字符串字面量

双引号

>>> spam = "This is Tom' cat"
>>> spam
"This is Tom' cat"

转义字符

>>> print('\',\",\t,\n,\\')
',",    ,
,\

原始字符串

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

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

用三重引号的多行字符串

>>> print('''Dear Tom:
Thank for your help
Tjanks;
Lili''')
Dear Tom:
Thank for your help
Tjanks;
Lili

字符串下标和切片

>>> spam = 'hello world!'
>>> spam[0]
'h'
>>> spam[4]
'o'
>>> spam[-1]
'!'
>>> spam[0:5]
'hello'
>>> spam[:5]
'hello'
>>> spam[6:]
'world!'

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

>>> 'hello' in 'hello word!'
True

有用的字符串方法

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

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

isX字符串方法

  • isalpha() 返回Ture,字符串只包含字母,并且非空
>>> 'hello'.isalpha()
True
>>> 'hello '.isalpha()
False
>>> 'hello1223'.isalpha()
False
  • isalnum() 返回Ture,字符串只包含数字和字母,并且非空
>>> 'hello'.isalnum()
True
>>> 'hello '.isalnum()
False
>>> 'h545'.isalnum()
True
  • isdecimal() 返回True,字符串只包含数字,并且非空
>>> '123'.isdecimal()
True
>>> 'hello123'.isdecimal()
False
  • isspace 返回True,字符串只包含空格、制表符、换行并且非空
>>> ' '.isspace()
True
  • istitle() 返回True,字符串仅以大写字母开头
>>> '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()

>>> '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()

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

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

>>> 'Hello'.rjust(10)
'     Hello'
>>> 'Hello'.rjust(20)
'               Hello'
>>> 'Hello'.ljust(10)
'Hello     '
>>> 'Hello '.center(10)
'  Hello   '
>>> 'Hello'.center(5)
'Hello'
#当字符不够时才不填充
>>> 'Hello'.center(4)
'Hello'

默认填充为空格

>>> 'Hello'.rjust(20, '*')
'***************Hello'
>>> 'Hello'.ljust(10, '-')
'Hello-----'
>>> 'Hello'.center(10, '+')
'++Hello+++'

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

>>> ' hello word '.strip()
'hello word'
>>> ' hello word'.lstrip()
'hello word'
>>> 'hello word '.rstrip()
'hello word'

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

>>> 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() 两个函数,可以向计算机的剪贴板发送文本、或者从它接收文本

>>> import pyperclip
>>> pyperclip.copy('Hello')
>>> pyperclip.paste()
'Hello'

小项目:口令保险箱

#! 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标记中添加无序列表

#! 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)

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/02/22/python-string-operations/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
Python字符串操作
字符串字面量 双引号 >>> spam = "This is Tom' cat" >>> spam "This is Tom' cat" 转义字符 >>> print(&#……
<<上一篇
下一篇>>
文章目录
关闭
目 录