phthon字符与list常用方法和属性
字符
methods :
- count:统计字符在字符串中出现的次数(return int)
[searchStr,startIndex,endIndex]
mypty = 'This is a demo of the count method of str'
print(mypty.count('i', 0, len(mypty))) # 2
# 找不到则返回0
print(mypty.count('z', 0, len(mypty))) # 0
- find 在字符串里找到第一个符合查找条件的字符的下标(return int)
[searchStr,startIndex,endIndex]
mypty = 'This is a demo of the find method of str'
print(mypty.find('i', 0, len(mypty))) # 2
# 找不到则返回-1
print(mypty.find('z', 0, len(mypty))) # -1
- replace 在字符串中替换指定的字符 (return new str)
[beforeStr,afterStr,replaceCount] replaceCount:默认全部替换
mypty = 'This is a demo of the replace method of str'
newMypty = mypty.replace('is', 'are')
print(newMypty) # Thare are a demo of the replace method of str
- upper & isupper : 将字符转换为全大写 & 字符内是否都是大写
upper : (return upper str)
isupper : return Boolean
mypty = 'this is a demo of the upper method of str'
print(mypty.upper()) # THIS IS A DEMO OF THE UPPER METHOD OF STR
newMypty = mypty.upper()
print(newMypty.isupper()) # True
- lower & islower : 将字符转换为全小写 & 字符内是否都是小写
lower : (return lower str)
islower : return Boolean
mypty = 'THIS IS A DEMO OF THE LOWER METHOD OF STR'
print(mypty.lower()) # this is a demo of the lower method of str
newMypty = mypty.lower()
print(newMypty.islower()) # True
- split : 将字符基于指定字符分割成一个list,没有参数则默认以空格为分割符
Return a list of the words in the string, using sep as the delimiter string.
[splitKey,splitCount]
return list
mypty = 'this is a demo of the split method of str'
print(mypty.split(' ')) # 基于空格分割 ['this', 'is', 'a', 'demo', 'of', 'the', 'split', 'method', 'of', 'str']
- strip : 去除一个字符前后两边的指定字符,不传参则去除两边空格(一侧有也可以替换,不穿参数则只能替换两侧空格)
如果想要替换中间的空格符,则可以使用
str.replace(' ','')
实现
Return a copy of the string with leading and trailing whitespace removed. If chars is given and not None, remove characters in chars instead.
return stripped str
# 没有参数
mypty = ' this is a demo of the split method of str '
print(mypty ) # ' this is a demo of the split method of str ' # 初始有两边空格
print(mypty.strip()) # 'this is a demo of the split method of str' 无两边空格
#有参数
mypty = 'zzzthis is a demo of the split method of strzzz'
print(mypty) # 'zzzthis is a demo of the split method of strzzz'
print(mypty.strip('zzz')) # 'this is a demo of the split method of str'
#错误参数,则返回原字符
print(mypty.strip('sss')) # 'zzzthis is a demo of the split method of strzzz'
8.格式化输出
- %s
语法:
str%s%{value}
其中s指占位符 ,后面的百分比后的大括号标识s对应的值
允许为数字、字符、字l典、list类型
demostr = '致%s'%('爱丽丝')
print(demostr) # 致爱丽丝
demostr = '致%s'%(1>2)
print(demostr) # 致False
demostr = '致%s'%([1,2,3])
print(demostr) # 致[1, 2, 3]
- %d
语法:
只接收数字类型%d
%(Number)
# 整型
numberStr = '只接受数字类型%d'%(269)
print(numberStr) # 只接受数字类型269
# 浮点型
doubleStr = '只接受数字类型且输出为整数(向下取整)%d'%(99.9)
print(doubleStr) # 只接受数字类型且输出为整数(向下取整)99
# 其他类型则会报错
numberStr = '只接受数字类型%d'%('哈哈哈')
print(numberStr) # TypeError
- %f
语法:`只接受数字类型%f'%(Number) 其中整型会保留六位小鼠
floatStr = '只接受数字类型%f'%(269)
print(floatStr) # 只接受数字类型269.000000
#有小数的依然保留六位
floatStr = '只接受数字类型%f'%(269.9)
print(floatStr) # 只接受数字类型269.000000
#超过六位,看第七位,第七位大于5则第六位进1且舍去剩余位数
## 进1
floatStr = '只接受数字类型%f'%(269.1111116)
print(floatStr) # 只接受数字类型269.111112
## 舍去
floatStr = '只接受数字类型%f'%(269.1111115)
print(floatStr) # 只接受数字类型269.111111
- F/f 表达式
name = 19
mystr = F'我的名字是{name}'
print(mystr) # 我的名字是19
未完待续
以上。