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!'

字符串中的innot in 操作符

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

有用的字符串方法

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

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

例子:

print('How are you')
feeling = input()
if feeling.lower() == 'great':
    print('I feel great too.')
else:
    print('I hope the rest of your day is good.')


RESTART: 
How are you
greAT
I feel great too.

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':'15735184252@163.com',
             'blog':'http://blog.csdn.net/mq_go',
             '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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值