Python core programming Chapter 6 after-school questions self-made answers, python Chapter 6

Source: Internet
Author: User

Python core programming Chapter 6 after-school questions self-made answers, python Chapter 6

6-6. string. create a string. substitute function of strip (): accepts a string and removes spaces between it and the end (if the string is used. * The strip () function makes this exercise meaningless)

 1 'Take a string and remove all leading and trailing whitespace' 2  3 def newStrip(str): 4     'delete blanks around a string' 5     _end = len(str) 6     _start = 0 7      8     # delete the blanks at the beginning of the string 9     for i in range(_end):10         if str[i] != ' ':11             _start = i12             break              13     else:14         print 'invalid: The string is a blank string.'     # if the string is a 15         _blank = True                                      # blank string16         17     18     # delete the blanks in the end of the string19     for i in range(-1, _start - _end, -1):20         if str[i] != ' ':21             _end = _end + i22             break      23     if not _blank:24         print '-' + str[_start: _end] + '-'   # print '-' make sure the function work25 26 # test27 if __name__ == '__main__':28     newStrip(raw_input('Enter a string: '))

 

6-7. Debug. Check the code (buggy. py) in example 6.5)

(A) Study this code and

Related Article

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.