Newbie string questions

2011-05-25 Thread Matty Sarro
Hey everyone,
This is a super noob question, so please be gentle.
I am working my way through Learn Python the Hard Way using both
python 2.7 and python 3.1 (I want to get a handle on the differences
between the two - the intention to write things in python 3 but be
able to understand things from python 2).

The first quarter or so of the book details lots of stuff about
strings. Most of the learning is by doing, with less of an emphasis on
the theory behind what certain things actually do. The issue is, I
can't seem to find some of the items in the documentation. Right now
what is stumping me... what exactly does %r do? I can't find it in the
documentation anywhere.
-Matty
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Newbie string questions

2011-05-25 Thread Chris Angelico
On Wed, May 25, 2011 at 11:06 PM, Matty Sarro msa...@gmail.com wrote:
 Right now what is stumping me... what exactly does %r do?

You're talking about the formatting operator? It's like the repr function:

http://docs.python.org/library/functions.html#repr

Chris Angelico
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Newbie string questions

2011-05-25 Thread Chris Guidry
On Wednesday, May 25, 2011 9:06:02 AM UTC-4, Matty Sarro wrote:
 can't seem to find some of the items in the documentation. Right now
 what is stumping me... what exactly does %r do? I can't find it in the
 documentation anywhere.

Matty, %r in a format string is very much like %s.  %s calls str(your_object) 
in order to produce the resulting string.  %r calls repr(your_object).  
Generally, you'd want to use %s for strings that will surface to a user, while 
%r is great for debugging and logging.

Also, if you haven't come across repr(), you should be able to find that in the 
docs; it's also considered good practice to overload repr() in your own classes 
in order to provide a useful representation for your objects.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Newbie string questions

2011-05-25 Thread Matty Sarro
Thanks guys! I appreciate it. I was wondering why %r was always
showing things enclosed in single-quotes.

On Wed, May 25, 2011 at 9:13 AM, Chris Guidry ch...@theguidrys.us wrote:
 On Wednesday, May 25, 2011 9:06:02 AM UTC-4, Matty Sarro wrote:
 can't seem to find some of the items in the documentation. Right now
 what is stumping me... what exactly does %r do? I can't find it in the
 documentation anywhere.

 Matty, %r in a format string is very much like %s.  %s calls str(your_object) 
 in order to produce the resulting string.  %r calls repr(your_object).  
 Generally, you'd want to use %s for strings that will surface to a user, 
 while %r is great for debugging and logging.

 Also, if you haven't come across repr(), you should be able to find that in 
 the docs; it's also considered good practice to overload repr() in your own 
 classes in order to provide a useful representation for your objects.
 --
 http://mail.python.org/mailman/listinfo/python-list

-- 
http://mail.python.org/mailman/listinfo/python-list


string questions

2007-09-15 Thread 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


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: string questions

2007-09-15 Thread Marc 'BlackJack' Rintsch
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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: string questions

2007-09-15 Thread J. Cliff Dyer
Marc 'BlackJack' Rintsch wrote:
 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.



-- 
http://mail.python.org/mailman/listinfo/python-list