Python core programming chapter sixth exercises

Source: Internet
Author: User

6-6 Creating a similar String.strip () function

Method an inefficient method for copying and generating substring objects in large numbers

def Str_strip (s):
While Len (s) >=2:
If s[0]== ':
S=s[1:]
Else
Break
While Len (s) >=2:
If s[-1]== ':
S=S[:-1]
Else
Break
If s== ' or s== ':
Return '
Else
return s

Method Two: Convert to List

def Str_strip (s):

    if s = = "or S = =" ":
Return ""
#to List
Elif Len (s) >=2:
L = List (s)
While L and l[0] = = "":
L.pop (0)
While L and l[-1] = = "":
L.pop (-1)
If L:
Return "". Join (L)
Else
Return ""
Else
return s

6-10.
String. Write a function that returns a string similar to the input string, requiring the case of the string to be reversed, for example, enter "Mr.ed" and should return "Mr.ed" as the output.

input = raw_input (' Please input a string: ... ')
Output = '
For I in input:
if i = = I.upper ():
Output = output + I.lower ()
Else
Output = output + I.upper ()
Print output

Python core programming chapter sixth exercises

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.