基础与字符串

数学运算

import math
a = [1, 2, 3, 4]
# 绝对值,最大值,最小值,求和,排序,求长度
print(abs(-1),max(a), min(a), sum(a), sorted(a), len(a))
# 求商和余数,求乘方,取指定位数的小数
print(divmod(6, 4), pow(2, 3), round(math.pi, 4))

输出相关

import random
for i in range(5):
    print(random.randint(1,10))
print('Hello',end='')
print('World')
print('cat','dog','pig')
print('cat','dog','pig',sep=',')

字符串处理

from loguru import logger
print(len('Hello'),str(22),int('33'),float(10))
str1='This is a test'
str1=str1.upper()
logger.info(str1)
str1=str1.lower()
logger.info(str1)
logger.info(str1.isupper())

logger.success(str1.islower())

logger.success(str1.upper().isupper())

文本加工

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)

print(pyperclip.paste())

与系统剪贴板交互信息

import pyperclip
pyperclip.copy('Pyper test')
print(pyperclip.paste())

删除两端空白或指定字符

a='   triptest '.strip()
b='   triptest '.lstrip()
c='   triptest '.rstrip()
d='ffssddaaBBBCCCasdfEEEddssaaff'.strip('asdf')
print(a,b,c,d)

文本对齐

'Hello'.rjust(10)
print('Hello'.rjust(7,'*'))
print('Hello'.ljust(10))
print('Hello'.ljust(20,'-'))
print('Hello'.center(10))
print('Hello'.center(20,'='))

字符串与列表的连接join和分割split

print('|'.join(['cats','rats','bats']))
print(''.join(['cats','rats','bats']))
str1='This is a test'
print(str1.split())
str1='This,is,a,test'
print(str1.split(','))
str1='''Two line
a example'''
print(str1.split('\n'))

字符串开始与结束部分检查判断

str1='This,is,a,test'
print(str1.startswith('This'))
print(str1.endswith('Test'))


不要害怕告别,要相信,四季更替,花开花落,都是命中注定。