Python常规操作:
#len() 获取字符串长度
length = len('abcde')
print('length'+ len)#length = 5
#eval() 去掉字符串的引号,把字符串变为int型等
str = input('请输入数据:') #输入123456
print(str) # str = '123456'
strA = eval(str)
print(strA) # strA = 123456
#str() 功能与eval()相反
#.upper()将字符串全变为大写
str = 'abcde'
print(str.upper()) #输出为: 'ABCDE'
#.lower() 将字符串全变为小写
str = 'ABCDE'
print(str.lower()) #输出为: 'abcde'
#.split(sqe = none) 将字符串根据sqe分割
'a,b,c'.split(',') #输出为: ['a','b','c']
#.count(sub) 返回子串sub在主串中出现的个数
'an apple a day'.count('a') #结果为: 4
#.replace(old, new) 将旧串用新串替换点
'Python'.replace('n', 'n123.io') #结果为: 'Python123.io'
#.center(wdith, fillchar) 字符串以width为宽度居中,用fillchar来填充
'Python'.center(20, '=')
#结果为: =======Python=======
#.strip(chars) 去掉字符串中左侧和右侧与chars相同的字符串
'=Python='.strip('=')
# 结果为: 'Python'
#.lstrip(chars) 去掉字符串中左侧的与chars相同的字符串
'=Python='.lstrip('=')
#结果为: 'Python='
#.rstrip(chars) 去掉字符产中右侧的与chars相同的字符串
'=Python='.rstrip('=')
#结果为: '=Py thon'
#.join(iter) 在字符串中处最后一个元素外都天界一个iter
'Python'.join(',')
#结果为:'P,y,t,h,o,n'
Python输出格式化
#字符串的格式化--槽
print('{}:计算机{}的CPU占用率为{}%'.format('2018-10-10', 'c', 10))
#结果为: 2018-10-10:计算机c的CPU占用率为10%
槽内部对格式化的配置方式
{<参数序号>:<格式配置标记>}
: | <填充> | <对齐> | <宽度> | <,> | <.精度> | <类型> |
---|---|---|---|---|---|---|
引导符号 | 用于填充的单个字符 | <左对齐;>右对齐;^居中对齐 | 数字的千位分隔符 | 小数浮点数精度或字符串最大输出长度 | 整数类型b,c,d,o,x,X,浮点数类型e,E,f,% |
#字符串的格式化--占位符
print('%S:计算机%c的CPU占用率为%d%',%('2018-10-10', 'c',10))
%d 对应整型变量
%c 对应字符型变量
%s 对应字符串型变量
str = 'World'
print('Hello',str)
#输出结果位: Hello World
str = 'world'
print('Hello'+str)
#输出结果为: HelloWorld