码迷,mamicode.com
首页 > 其他好文 > 详细

20170417学习find、replace、abs三个函数

时间:2017-04-17 11:51:25      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:lame   word   text   第一个   pos   int   search   编程   new   

1.编程python学习了三个函数

①find(str,pos_start,pos_end)

解释:
  • str:被查找“字串”
  • pos_start:查找的首字母位置(从0开始计数。默认:0)
  • pos_end: 查找的末尾位置(默认-1)
返回值:如果查到:返回查找的第一个出现的位置。否则,返回-1。
search = ‘168‘
num_a = ‘1386-168-0006‘
num_b = ‘1681-222-0006‘
print(search + ‘ is at ‘ + str(num_a.find(search)) + ‘ to ‘ + str(num_a.find(search) + len(search)) + ‘ of num_a‘)
②replace(old, new, max)
解释:
  • old:将被替换的子字符串
  • new:新字符串,用于替换old子字符串
  • max: 可选字符串, 替换不超过 max 次
  • print str.replace("is", "was", 3);
def text_filter(word,censored_word = ‘lame‘,changed_word = ‘Awesome‘):
return word.replace(censored_word,changed_word)
print(text_filter(‘Python is lame!‘))#实现过滤,lame替换为Awesome

③abs()函数

解释:返回数字的绝对值

print "abs(-45) : ", abs(-45)
abs(-45) : 45

20170417学习find、replace、abs三个函数

标签:lame   word   text   第一个   pos   int   search   编程   new   

原文地址:http://www.cnblogs.com/Jiang190/p/6721899.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!