Think Python X8: Difference between revisions

From XPUB & Lens-Based wiki
(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...)
 
Line 16: Line 16:
</source>
</source>


So that *all* characters in old get replaced by new in string,
So that ''all'' characters in old get replaced by new in string,
for instance:
for instance:
<source lang="python">
<source lang="python">

Revision as of 12:00, 11 November 2008

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