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