6.1 处理字符串cookie
6.2 字符串方法upper()、lower()、isupper()、和islower()app
upper()和lower()字符串方法返回一个新的字符串,其中原字符串的全部字母都被相应地转换为大写或小写。字符串中的非字母字符保持不变。函数
>>> spam='hello world' >>> spam.upper() 'HELLO WORLD' >>> spam 'hello world' >>> spam =spam.upper() >>> spam 'HELLO WORLD' #用islower(),isupper()来判断字符串中的字母是否都是小写和大写。 >>> spam.islower() False >>> spam.isupper() True #这样调用也是可行的 >>> 'hello'.upper() 'HELLO' >>> 'HELLO'.lower() 'hello'
这些方法没有改变字符串自己,而是返回一个新的字符串spa
6.2.1 isX字符串方法code
该程序反复询问用户年龄和口令,知道他们输入一个有效的值:ip
while True: print('Enter your age:') age =input() if age.isdecimal(): break print('please enter a number for you age') while True: print('select a new password (letters and numbers only):') password = input() if password.isalnum(): break print('passwords can only have letters and numbers.')
6.2.2 字符串方法startswith()和endswith()ci
startswith()和endswith()方法,若是他们调用的字符串以该方法传入的字符串开始或结束,则返回True,反之则返回False。若是要检查字符串的开始或结束部分是否等于另外一个字符串,而不是整个字符串,这些方法就能够替代等于操做符==,这颇有用。字符串
6.2.3 字符串方法join()和split()input
join()方法在一个字符串上调用,参数是一个字符串列表,返回一个字符串。返回的字符串由传入的列表中每一个字符串链接而成。it
>>> ','.join(['cat','rat','bat']) 'cat,rat,bat' >>> ''.join(['my','name','is','Simon']) 'mynameisSimon' >>> ' '.join(['my','name','is','Simon']) 'my name is Simon' >>> 'ABC'.join(['my','name','is','Simon']) 'myABCnameABCisABCSimon' >>>
split()方法作的事情正好相反,它针对一个字符串调用,返回一个字符串列表。
>>> 'my name is Simon'.split() ['my', 'name', 'is', 'Simon'] >>> 'myABCnameABCisABCSimon'.split("ABC") ['my', 'name', 'is', 'Simon']
默认状况下,字符串'my name is Simon'按照空白字符分割,诸如空格、制表符或换行符。
6.2.4 用rjust()、ljust()和center()方法对齐文本
rjust()和ljust()字符串方法返回调用他们的字符串的填充版本,经过插入空格来对齐文本。这两个方法的
center()字符串方法与ljust()和rjust()相似,但他让文本居中。
>>> 'hello'.rjust(10) ' hello' >>> 'hello'.ljust(20) 'hello ' >>> 'hello'.rjust(20,'*') '***************hello' >>> 'hello'.ljust(20,'*') 'hello***************' >>> 'hello'.center(10,'=') '==hello==='
'hello'.rjust(10)是要右对齐,将'hello'放在一个长度为10的字符串中'hello'有5个字符他会在左边加5个空格,获得一个10个字符的字符串。
这个是一个打印表格式数据,流出空格的小代码:
def printPicnic(itemsDict, leftWidth, rightWidth): print('PICNIC ITEMS'.center(leftWidth + rightWidth, '-')) for k, v in itemsDict.items(): print(k.ljust(leftWidth, '.') + str(v).rjust(rightWidth)) picnicItems = {'sandwiches': 4, 'apples': 12, 'cups': 4, 'cookies': 8000} printPicnic(picnicItems, 12, 5) printPicnic(picnicItems, 20, 6)
运行结果以下:
---PICNIC ITEMS-- sandwiches.. 4 apples...... 12 cups........ 4 cookies..... 8000 -------PICNIC ITEMS------- sandwiches.......... 4 apples.............. 12 cups................ 4 cookies............. 8000
6.2.5 用strip()、rstrip()和lstrip()删除空白字符
有一个可选的字符串参数,指定两边的那些字符应该删除。
>>> spam = 'spamspamspambaconspameggsspamspam' >>> spam.strip('spam') 'baconspamegg'
向strip()方法传入参数'spam',告诉它在变量中存储的字符串两端删除出现的s、p、a、m。传入strip()方法的字符串中的字符顺序不重要strip('spam')和strip('mpsa')作的事情同样
6.2.6 pyperclip()模块拷贝粘贴字符串
pyperclip模块有copy()和paste()函数,它能够像计算机的剪贴板发送文本,或从它接收文本。将程序的输出发送到剪贴板,使他很容易粘贴到邮件,文字处理程序或其余软件中。
实践项目 在wiki标记中添加无序列表
import pyperclip text = pyperclip.paste() lines = text.split('\n') for i in range(len(lines)): lines[i] = '*'+ lines[i] text='\n'.join(lines) pyperclip.copy(text)