欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python快速入门(2)练习题

程序员文章站 2022-04-19 23:16:46
# c. fix_start # given a string s, return a string # where all occurences of its first cha...
# c. fix_start
# given a string s, return a string
# where all occurences of its first char have
# been changed to '*', except do not change
# the first char itself.
# e.g. 'babble' yields 'ba**le'
# assume that the string is length 1 or more.
# hint: s.replace(stra, strb) returns a version of string s
# where all instances of stra have been replaced by strb.
		def fix_start(s):
		  # +++your code here+++
		  # lab(begin solution)
		  front = s[0]
		  back = s[1:]
		  fixed_back = back.replace(front, '*')
		  return front + fixed_back


# e. not_bad
# given a string, find the first appearance of the
# substring 'not' and 'bad'. if the 'bad' follows
# the 'not', replace the whole 'not'...'bad' substring
# with 'good'.
# return the resulting string.
# so 'this dinner is not that bad!' yields:
# this dinner is good!

		def not_bad(s):
		  # +++your code here+++
		  # lab(begin solution)
		  n = s.find('not')
		  b = s.find('bad')
		  if n != -1 and b != -1 and b > n:
			s = s[:n] + 'good' + s[b+3:]
		  return s