47 lines
586 B
Python
47 lines
586 B
Python
# 1、str.replace(old,new,count) 返回str副本 old->new 替换count次数 count可以省略
|
|
str='hello,zhangsan,lisi,wangwu,lisi,hello,zhangsan'
|
|
print(str.replace('hello','嗨'))
|
|
print(str.replace('hello','嗨',1))
|
|
print(str.replace('hello',' '))
|
|
|
|
# 2、str.center(width,fillchar) 字符串居中函数 第二个参数为宽度不足填充符号可以省略
|
|
str='hello'
|
|
print(str.center(10,'*'))
|
|
print(str.center(21,'#'))
|
|
# 三角形
|
|
str=''
|
|
for i in range(1,10,2):
|
|
print(str.center(i,'*'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|