CSC108H5 Lecture Notes - Lecture 10: Try This

27 views2 pages
School
Course
i = 0
while i < len(s):
if (i % 2 == 0):
new_s += s[i]
#else: this is redundant
# new_s = new_s # does this do anything?
i += 1
return new_s
def every_other3(s):
"""
(str) -> s
Given a string s, return a new string that is
made up of every other character in s.
>>> every_other3('batman')
'bta'
"""
new_s = ''
i = 0
while i < len(s):
new_s += s[i]
i += 2
return new_s
def every_other4(s):
"""
(str) -> s
Given a string s, return a new string that is
made up of every other character in s.
>>> every_other4('batman')
'bta'
"""
return s[::2] # this is same as s[0:len(s):2]
def change_first(s, c):
"""
Unlock document

This preview shows half of the first page of the document.
Unlock all 2 pages and 3 million more documents.

Already have an account? Log in
897wjface and 39475 others unlocked
CSC108H5 Full Course Notes
12
CSC108H5 Full Course Notes
Verified Note
12 documents

Document Summary

I = 0 while i < len(s): if (i % 2 == 0): new_s += s[i] # new_s = new_s # does this do anything? i += 1 return new_s def every_other3(s): (str) -> s. "bta" new_s = "" i = 0 while i < len(s): new_s += s[i] i += 2 return new_s def every_other4(s): (str) -> s. Given a string s, return a new string that is made up of every other character in s. "bta" return s[::2] # this is same as s[0:len(s):2] def change_first(s, c): (str, str) -> s. Change the first character of s to c and return this new string. # i cannot do this ^ strings are immutable return c + s[1:] def uncapitalize(s): (str) -> s. Given a string s, return that same string uncapitalized. # use help(str) to check all the methods def capitalize(s): (str) -> s. Given a string s, return that same string capitalized.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents

Related Questions