首页 > 编程语言 > 详细

python的学习3

时间:2017-12-09 19:02:00      阅读:212      评论:0      收藏:0      [点我收藏+]

1、解决用户输入空行的办法使用startwith判断或者len(strs)>0

技术分享图片

2、字符串运用方法中strip和replace特别有用。

Python strip() 方法用于移除字符串头尾指定的字符(默认为空格)。语法:

str.strip([chars]);
#!/usr/bin/python

str = "0000000this is string example....wow!!!0000000";
print str.strip( ‘0‘ );

#输出结果为
this is string example....wow!!!

  只移除字符串头尾指定的字符,中间部分不会移除:

#!/usr/bin/python
str = "0000000this is string 0000example....wow!!!0000000"; print str.strip( ‘0‘ ); 结果为: this is string 0000example....wow!!!

Python replace() 方法把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次。语法:

str.replace(old, new[, max])
#!/usr/bin/python

str = "this is string example....wow!!! this is really string";
print str.replace("is", "was");
print str.replace("is", "was", 3);

结果为:
thwas was string example....wow!!! thwas was really string
thwas was string example....wow!!! thwas is really string

#old -- 将被替换的子字符串。
#new -- 新字符串,用于替换old子字符串。
#max -- 可选字符串, 替换不超过 max 次

3、



  

 

python的学习3

原文:http://www.cnblogs.com/HuangDaDa/p/8012097.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!