Python Quick Start (2) exercise questions

Source: Internet
Author: User

# C. fix_start
# Given a string s, return a string
# Where all occurences of its first char have
# Been changed to '*', does t 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
# 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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.