Python编程让繁琐的工作自动化(11)-字符串操作

#字符串字面量
spam = "This is Alice's cat."
spam = 'this is "hello" word!.'
#转义字符
spam = 'Say hi to Bob\'s mother'
#原始字符串
print(r'That is Carlo\'s cat.')
#用三重引号的多行字符串
print(''' Dear Alice,
Eve's cat has been arrested for catnapping, cat butglary, and extortion.
Sincerely,
Bob''')
#多行字符串用作注释
"""
This is a test Python program.
Written by OUC
This program was designed for python3.
"""
def spam():
""" This is a multiline comment to help
explain what the spam() function does."""
print('hello')
#字符串下标和切片
spam = 'hello world'
spam[0]
spam[-1]
spam[3:4]
spam[::-1]
#字符串的in和not in操作符
'hello' in 'hello world'#True
'Hello' in 'hello world'#False
#常用的字符串方法
#upper,lower
spam = 'Hello world!'
spam = spam.upper()//'HELLO WORLD'
#注意,upper,lower返回一个副本,不会改变原始字符串。常用于进行大小无关的比较
#isX方法
spam.islower()//False
'HELLO'.isupper()//True
'hello123'.isalpha()//False
'hello123'.isalnum()//True,字母和数字
'hello'.isalnum()//True
'123'.isdecimal()//True,如果只包含数字字符,返回True
' '.isspace()//True,如果只包含空格,制表符,换行符,并且非空
'This Is Title Case'.istitle()//True,如果只包含大写字母开头,后面都是小写字母的单词
'This Is Title Case 123'.istitile()//True
'This Is not Title Case'.istitle()//False
'This Is NOT Title Case Either'.istitle()//False
#可以用来验证用户输入
while True:
    print('enter your age: ')
    age = input()
    if age.decimal():
        break
    print('Please enter a number for your age.')
#startwith和endwith方法
'hello world'.startwith('hello')//True,如果以传入的字符串开头则True
'helloworld.txt'.endwith('.txt')//True
#join和split方法
', '.join(['cats', 'rats', 'bats'])//'cats, rats, bats',将字符串列表用开头的格式连接起来
'My name is Simon'.split()//['My', 'name', 'is', 'Simon'],将字符串分割成字符串列表,默认按空格分割,可以传参数
'My, name, is, Simon'.split(',')//['My', 'name', 'is', 'Simon']
#一种常见的用法是按照换行符分割多行字符串
spam = ''' Dear Alice,
Eve's cat has been arrested for catnapping, cat butglary, and extortion.
Sincerely,
Bob''')
>>> spam.split('\n')
[" Dear Alice,Eve's cat has been arrested for catnapping, cat butglary, and extortion.Sincerely,Bob"]
#rjust,ljust,center方法对齐文本
'hello'.rjust(10)//'     hello'
'hello'.rjust(10, '*')//'*****hello',第二个参数指定填充字符
#strip,rstrip,lstrip方法删除空白字符
spam = ' Hello World'
spam.strip()//'Hello world',删除两侧空白
#传入字符串参数可以删除两边的字符
spam = 'SpamBaconpamS'
spam.strip('ampS')//'Bacon',删除两侧出现的a、m、p、S,顺序不重要
#用pyperclip模块拷贝粘贴字符串
import pyperclip
pyperclip.copy('Hello world'),复制到剪贴板
pyperclip.paste()//'hello world',如果剪贴板的内容被别的软件修改,则返回的是修改后的

项目:账户密码保管箱

对于不同的网站会有不同的密码,而记住这些账户密码是很烦人的事情,而如果采用同样的密码会有较大的风险。
最好是在你的计算机上,使用口令管理器软件,利用一个主控口令,解锁口令管理器,然后将某个账户密码拷贝到剪贴板,再粘贴到输入框。

#! python3
#pw.py-An insecure password locker program.
Passwords = {'email':'fjaldfjdlafjdsafljaf',
             'blog':'fdksgjlgjoirgjld'}

import sys, pyperclip
if len(sys.argv) < 2:
    print('Usage:py pw.py [account] - copy account password')
    sys.exit()//提前退出,需要sys模块
account = sys.argv[1]#first command line arg is the account name

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

你可能感兴趣的:(python)