@zhangyu756897669
2017-09-01T11:33:18.000000Z
字数 2736
阅读 576
python
官方文档
'Hello'.rjust(10)
' Hello'
'Hello World'.rjust(20)
' Hello World'
'Hello'.ljust(15)
'Hello '
'Hello'.rjust(20, '*')
'***************Hello'
'Hello'.ljust(20, '-')
'Hello---------------'
'Hello'.center(20)
' Hello '
'Hello'.center(20, '*')
'*****Hello******'
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
使用rjust(),ljust()和center()可以确保字符串整齐对齐,即使您不确定字符串长度有多少字符。
有时您可能想从字符串的左侧,右侧或两边剥离空格字符(空格,制表符和换行符)。 strip()string方法将返回一个新的字符串,而不含开头或结尾的任何空白字符。 lstrip()和rstrip()方法将分别从左端和右端删除空白字符。
spam = ' Hello World '
spam.strip()
'Hello World'
spam.lstrip()
'Hello World '
spam.rstrip()
' Hello World'
spam = 'SpamSpamBaconSpamEggsSpamSpam'
spam.strip('ampS')
'BaconSpamEggs'
import pyperclip
pyperclip.copy('Hello world!')
pyperclip.paste()
'Hello world!'
您可能在许多不同的网站上有帐户。对于每一个使用相同的密码来说,这是一个不好的习惯,因为如果这些网站有任何安全漏洞,黑客将会向所有其他帐户学习密码。最好在计算机上使用密码管理器软件,使用一个主密码来解锁密码管理器。然后,您可以将任何帐户密码复制到剪贴板,并将其粘贴到网站的密码字段中。
您在此示例中创建的密码管理器程序不安全,但它提供了这样的程序如何工作的基本演示。
PASSWORDS = {'email': 'F7minlBDDuvMJuxESSKHFhTxFtjVB6',
'blog': 'VmALvQyKAxiVH5G8v01if1MLZF3sdt',
'luggage': '12345'}
PASSWORDS = {'email': 'F7minlBDDuvMJuxESSKHFhTxFtjVB6',
'blog': 'VmALvQyKAxiVH5G8v01if1MLZF3sdt',
'luggage': '12345'}
import sys
if len(sys.argv) < 2:
print('Usage: python pw.py [account] - copy account password')
sys.exit()
account = sys.argv[1] # first command line arg is the account name