string questions

S

Shawn Minisall

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!

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?

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?

Thanks.

-Shawn
 
M

Marc 'BlackJack' Rintsch

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
 
J

J. Cliff Dyer

Marc said:
But please don't use the functions in `string` that are also available as
methods on strings. Those functions are deprecated.
Meaning (for newbie clarification):

instead of string.upper(s2), just do s2.upper(). For more detail, see
the docs.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,266
Messages
2,571,075
Members
48,772
Latest member
Backspace Studios

Latest Threads

Top