string questions

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Sat Sep 15 20:00:09 EDT 2007


On Sat, 15 Sep 2007 19:52:47 -0400, Shawn Minisall wrote:

> Hi everyone, I'm a beginning programming student in Python and have a 
> few questions regarding strings.
> 
> If s1 = "spam"
> 
> If s2 = "ni!"
> 
> 1. Would string.ljust(string.upper(s2),4) * 3 start it at the left 
> margin and move it 12 spaces to the right because of the 4 *3?  If so, 
> why is it in the parathesis for the upper command and not the ljust?   I 
> already know that it would cap it to NI!

Fire up the interpreter and just try it.  And then the different parts to
know whats going on in detail.

But please don't use the functions in `string` that are also available as
methods on strings.  Those functions are deprecated.

> 2.  To get the output "Spam Ni! Spam Ni! Spam Ni!" I could do something 
> like this string.join ([s1, s2]),
> 
> But I'm a little lost how to get it repeated three times on one line.  
> Would I  just have to put the same command on the next two lines?

Try out the code from 1. and you should get an idea ho to repeat three
times.

> 3. To change spam to spm, the string.replace seems to be the best 
> function to use.  However, when I use
> string.replace(s1, "a", " ") in python to replace a with an empty space, 
> it doesn't work...I just get spam back when I print s1.   Any ideas?

Yes, read the documentation to find out that `replace()` does not alter the
string -- strings in Python are immutable -- but returns a new, changed
string.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list