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

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
原文地址:https://www.cnblogs.com/Jiang190/p/6721899.html