[Tutor] some string operations

Alan Gauld alan.gauld at btinternet.com
Sat Oct 4 10:31:55 CEST 2008


"David" <ldl08 at gmx.net> wrote

> I have two solutions:
>
> a)
> (string.capitalize(s1) + " " + string.capitalize( s2) + "  " ) * 3
> b)
> "%s %s " % (string.capitalize(s1), string.capitalize(s2)) * 3
>
> Both seem to work, but they seem overly complex. Where could I 
> improve?

Personally I'd go with b but avoid the string module and add an 
rstrip():

("%s %s " % (s1.capitalize(),s2.capitalize()) * 3).rstrip()

Or another approach:

"%s %s %s %s %s %s" % ((s1.capitalize(),s2.capitalize())*3)
'Foo Bar Foo Bar Foo Bar'

But the first is more flexible IMHO.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 




More information about the Tutor mailing list