字符串操作

2018-06-06  本文已影响9人  _大_兵_
>>> print(r'That is Carol\'s cat.')
That is Carol\'s cat.
>>> spam = 'Hello world!'
>>> spam[0]
'H'
>>> spam[:5]
'Hello'
>>> 'Hello' in 'Hello world!'
True
>>> 'Hello' not in 'Hello world'
False
print('How are you?')
feeling = input()
if feeling.lower() == 'great':
    print('I feel great too.')
else:
    print('I hope the rest of your dat is good.')

isupper()、islower()判断非空字符串的所有字母都是大写或小写,返回布尔值。

>>> spam = 'Hello world!'
>>> spam.islower()
False
>>> spam.lower().islower()
True

isX字符串方法

  • isalpha()字符串只包含字母并且非空,返回True
  • isalnum()字符串包含字母和数字并且非空,返回True
  • isdecimal()字符串只包含数字字符并且非空,返回True
  • isspace()字符串只包含空格、制表符和换行符并且非空,返回True
  • istitle()字符串仅包含以大写字母开头、后面都是小写字母的单词,返回True

如果需要验证用户输入,isX字符串方法是有用的,例如下面的程序反复询问用户年龄和口令,直到提供有效的输入。

while True:
    print('Enter your age:')
    age = input()
    if age.isdecimal():
        break
    print('Please enter a number of your age.') # else语句的简写

while True:
    print('Select a new password (letters and number only):')
    password = input()
    if password.isalnum():
        break
    print('Password can only have letters and numbers.')
>>> 'Hello world'.startswith('Hello')
True
>>> 'Hello world'.endswith('world')
True
>>> ','.jion(['cats','rats','bats'])
'cats,rats,bats'
>>> ' ' .jion(['My','name','is','Simon])
'My name is Simon'
>>> 'My name is Simon'.split()
['My','name','is','Simon']
>>> 'My name is Simon'.split()
['My na','e is Si','on']

*rjust()、ljust()和center()

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

如果需要打印表格式数据,流出正确的空格,这些方法就很有用。

def printpicnic(itemsDict,leftWith,rightWidth):
    print('PICNIC ITEMS'.center(leftWith+rightWidth,'-'))
    for k,v in itemsDict.items():
        print(k.ljust(leftWith,'.') + str(v).rjust(rightWidth,'.'))

picnicItems = {'sandwiches':4,'apples':12,'cups':4,'cookies':8000}
printpicnic(picnicItems,12,5)

输出
---PICNIC ITEMS--
sandwiches......4
apples.........12
cups............4
cookies......8000
>>> spam = ' Hello world! '
>>> spam.strip()
'Hello world!'
>>> spam.rstrip()
' Hello world!'
>>> spam.lstrip()
'Hello world! '
import pyperclip
pyperclip.copy('hello world!')
print(pyperclip.paste())

输出:
hello world!
#! python3
# spam.py
# path = 'E:\我的坚果云\python_work\实用'
PASSWORDS = {'email':'ASDFSASDFJ',
            'dabing':'asdf132'
            }

import sys,pyperclip
if len(sys.argv) < 2:
    print('Usage: py spam.py [account] - copy account password')
    sys.exit()

account = sys.argv[1]

if account in PASSWORDS:
    pyperclip.copy(PASSWORDS[account])
    print('password for ' + account + ' copied to clipboard.')
else:
    print('there is no account named ' + account)

2.创建bat批处理文件,放到代码文件所在的文件夹下,内容如下:

@python.exe E:\我的坚果云\python_work\实用\spam.py %*
@pause

3.设置环境变量,将bat文件所在路径加入Path变量中
4.win + R 打开运行窗口,输入 “sapm dabing”,点击“确定”,会跳出提示窗口,并将密码复制到剪贴板上,如图:


image.png
image.png
#! python3
# bulletPointAdder.py
# path = E:\我的坚果云\python_work\实用\bulletPointAdder.py

import pyperclip
text = pyperclip.paste()

# Separate lines and add stars.
lines = text.split('\n')

for i in range(len(lines)):
    lines[i] = '* ' + lines[i]
text = '\n'.join(lines)
pyperclip.copy(text)

1.复制文本
2.运行代码文件
3.粘贴处理好的文本

上一篇下一篇

猜你喜欢

热点阅读