[Tutor] some string operations
ldl08 at gmx.net
Sat Oct 4 10:03:50 CEST 2008
one of the exercises in Zelle's book is:
s1 = "spam"
s2 = "ni!"
show a Python expression that could construct the following result by
performing string operations on s1 and s2:
"Spam Ni! Spam Ni! Spam Ni!".
I have two solutions:
(string.capitalize(s1) + " " + string.capitalize( s2) + " " ) * 3
"%s %s " % (string.capitalize(s1), string.capitalize(s2)) * 3
Both seem to work, but they seem overly complex. Where could I improve?
Also, Python returns:
'Spam Ni! Spam Ni! Spam Ni! '
Which is not exactly "Spam Ni! Spam Ni! Spam Ni!" (note the final free
space in my outcome). I am at a loss as to how to perfect my answer with
regard to this issue.
Many thanks for helping me in my pythonic adventures!
More information about the Tutor