Think Python X8

From XPUB & Lens-Based wiki
Revision as of 12:00, 11 November 2008 by Michael Murtaugh (talk | contribs) (New page: == Exercise 8.x.1 == Strings have a function called replace: <source lang="python"> S.replace (old, new[, count]) -> string </source> Return a copy of string S with all occurrences of s...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Exercise 8.x.1

Strings have a function called replace:

S.replace (old, new[, count]) -> string

Return a copy of string S with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.

Write a function removechars:

replacechars(old, new, string)

So that *all* characters in old get replaced by new in string, for instance:

replacechars("aeiou", "", "Tomato")
=>
Tmt
	
replacechars("aeiou", "*", "Tomato")
=>
T*m*t*

Exercise 8.x.2