Moving from Python 2 to Python 3: A 4 page "cheat sheet"

Discussion in 'Python' started by Mark Summerfield, Dec 1, 2009.

  1. I've produced a 4 page document that provides a very concise summary
    of Python 2<->3 differences plus the most commonly used new Python 3
    features. It is aimed at existing Python 2 programmers who want to
    start writing Python 3 programs and want to use Python 3 idioms rather
    than those from Python 2 where the idioms differ.

    It uses Python 3.1 syntax since that looks like being the standard for
    a few years in view of the language moratorium.

    The document is U.S. Letter size but will also print fine on A4
    printers.

    It is available as a free PDF download (no registration or anything)
    from InformIT's website. Here's the direct link:
    http://ptgmedia.pearsoncmg.com/imprint_downloads/informit/promotions/python/python2python3.pdf

    And of course, if you want more on Python 3, there's always the
    documentation---or my book:)
    "Programming in Python 3 (Second Edition)" ISBN-10: 0321680561.
    Mark Summerfield, Dec 1, 2009
    #1
    1. Advertising

  2. > I've produced a 4 page document that provides a very concise summary
    > of Python 2<->3 differences plus the most commonly used new Python 3
    > features. It is aimed at existing Python 2 programmers who want to
    > start writing Python 3 programs and want to use Python 3 idioms rather
    > than those from Python 2 where the idioms differ.
    >
    > It uses Python 3.1 syntax since that looks like being the standard for
    > a few years in view of the language moratorium.


    This really looks very useful, thanks a lot!
    I've been wishing something like this existed for a while, really handy.

    Cheers,
    Daniel



    --
    Psss, psss, put it down! - http://www.cafepress.com/putitdown
    Daniel Fetchinson, Dec 1, 2009
    #2
    1. Advertising

  3. Mark Summerfield

    Gnarlodious Guest

    On Dec 1, 7:03 am, Mark Summerfield wrote:

    > "Programming in Python 3 (Second Edition)" ISBN-10: 0321680561.


    I ordered it...

    -- Gnarlie
    http://Gnarlodious.com
    Gnarlodious, Dec 1, 2009
    #3
  4. On Dec 1, 2:03 pm, Mark Summerfield <> wrote:
    > I've produced a 4 page document that provides a very concise summary
    > of Python 2<->3 differences plus the most commonly used new Python 3
    > features.


    Very nice indeed!

    My only quibble is with the statement on the first page that
    the 'String % operator is deprecated'. I'm not sure that's
    true, for all values of 'deprecated'. There don't appear
    to be any definite plans for getting rid of it just yet.

    Mark
    Mark Dickinson, Dec 1, 2009
    #4
  5. Mark Summerfield

    Lie Ryan Guest

    On 12/2/2009 1:03 AM, Mark Summerfield wrote:
    > I've produced a 4 page document that provides a very concise summary
    > of Python 2<->3 differences plus the most commonly used new Python 3
    > features. It is aimed at existing Python 2 programmers who want to
    > start writing Python 3 programs and want to use Python 3 idioms rather
    > than those from Python 2 where the idioms differ.
    >
    > It uses Python 3.1 syntax since that looks like being the standard for
    > a few years in view of the language moratorium.
    >
    > The document is U.S. Letter size but will also print fine on A4
    > printers.
    >
    > It is available as a free PDF download (no registration or anything)
    > from InformIT's website. Here's the direct link:
    > http://ptgmedia.pearsoncmg.com/imprint_downloads/informit/promotions/python/python2python3.pdf
    >
    > And of course, if you want more on Python 3, there's always the
    > documentation---or my book:)
    > "Programming in Python 3 (Second Edition)" ISBN-10: 0321680561.


    Nice.

    I suggest changing the lambda example a bit, the current example says:
    Python 2 Python 3
    lambda (a,b): a + b lambda t: t[0] + t[1]
    lambda a, b: a + b

    into something like:

    Python 2 Python 3
    lambda (a,b),c: a + b + c lambda t, c: t[0] + t[1] + c
    lambda a, b, c: a + b + c

    it is unclear at first sight that it refers to tuple argument unpacking.
    There should also some mention that tuple argument unpacking for regular
    function (def) is also gone.



    Also, I'm not sure what this change is referring to:
    Python 2 Python 3
    L = list(seq) L = sorted(seq)
    L.sort()

    L.sort is still available in python, and sorted() have been available
    since python 2. Both list.sort() and sorted() are for different purpose,
    and neither will be deprecated. What's the change here?
    Lie Ryan, Dec 1, 2009
    #5
  6. Mark Summerfield

    Terry Reedy Guest

    Mark Summerfield wrote:
    > I've produced a 4 page document that provides a very concise summary
    > of Python 2<->3 differences plus the most commonly used new Python 3
    > features. It is aimed at existing Python 2 programmers who want to
    > start writing Python 3 programs and want to use Python 3 idioms rather
    > than those from Python 2 where the idioms differ.
    >
    > It uses Python 3.1 syntax since that looks like being the standard for
    > a few years in view of the language moratorium.
    >
    > The document is U.S. Letter size but will also print fine on A4
    > printers.
    >
    > It is available as a free PDF download (no registration or anything)
    > from InformIT's website. Here's the direct link:
    > http://ptgmedia.pearsoncmg.com/imprint_downloads/informit/promotions/python/python2python3.pdf
    >
    > And of course, if you want more on Python 3, there's always the
    > documentation---or my book:)
    > "Programming in Python 3 (Second Edition)" ISBN-10: 0321680561.


    What might be even *more* helpful, with contributions from others
    perhaps, would be an indication of which changes are handled
    automatically by 2to3.py and which must be done by hand.

    tjr
    Terry Reedy, Dec 1, 2009
    #6
  7. Mark Summerfield

    John Bokma Guest

    Mark Summerfield <> writes:

    > It is available as a free PDF download (no registration or anything)
    > from InformIT's website. Here's the direct link:
    > http://ptgmedia.pearsoncmg.com/imprint_downloads/informit/promotions/python/python2python3.pdf


    Thanks!

    > And of course, if you want more on Python 3, there's always the
    > documentation---or my book:)
    > "Programming in Python 3 (Second Edition)" ISBN-10: 0321680561.


    Meh, second edition already? Haven't read the entire first edition
    yet. Which is IMO a good book (I also gave it to my brother as a
    present).

    Only negative point (to me) so far is that in the beginning (p8-9) the
    book mentions placing Python programs in C:\py3eg which gives me the
    unpleasant feeling that someone is coding on Windows XP with
    Administrator rights...

    Anyway, will add the 2nd edition to my wish list and donate the current
    one to the library in Xalapa (USBI) if they want it :)

    John
    John Bokma, Dec 1, 2009
    #7
  8. On 1 Dec, 17:50, Mark Dickinson <> wrote:
    > On Dec 1, 2:03 pm, Mark Summerfield <> wrote:
    >
    > > I've produced a 4 page document that provides a very concise summary
    > > of Python 2<->3 differences plus the most commonly used new Python 3
    > > features.

    >
    > Very nice indeed!
    >
    > My only quibble is with the statement on the first page that
    > the 'String % operator is deprecated'.  I'm not sure that's
    > true, for all values of 'deprecated'.  There don't appear
    > to be any definite plans for getting rid of it just yet.
    >
    > Mark


    I didn't make this up:)

    According to http://docs.python.org/dev/3.0/whatsnew/3.0.html
    "The plan is to eventually make this the only API for string
    formatting, and to start deprecating the % operator in Python 3.1."
    Mark Summerfield, Dec 2, 2009
    #8
  9. On 1 Dec, 18:30, Lie Ryan <> wrote:
    > On 12/2/2009 1:03 AM, Mark Summerfield wrote:
    >
    >
    >
    > > I've produced a 4 page document that provides a very concise summary
    > > of Python 2<->3 differences plus the most commonly used new Python 3
    > > features. It is aimed at existing Python 2 programmers who want to
    > > start writing Python 3 programs and want to use Python 3 idioms rather
    > > than those from Python 2 where the idioms differ.

    >
    > > It uses Python 3.1 syntax since that looks like being the standard for
    > > a few years in view of the language moratorium.

    >
    > > The document is U.S. Letter size but will also print fine on A4
    > > printers.

    >
    > > It is available as a free PDF download (no registration or anything)
    > > from InformIT's website. Here's the direct link:
    > >http://ptgmedia.pearsoncmg.com/imprint_downloads/informit/promotions/...

    >
    > > And of course, if you want more on Python 3, there's always the
    > > documentation---or my book:)
    > > "Programming in Python 3 (Second Edition)" ISBN-10: 0321680561.

    >
    > Nice.


    Thanks!

    > I suggest changing the lambda example a bit, the current example says:
    > Python 2                      Python 3
    > lambda (a,b): a + b           lambda t: t[0] + t[1]
    >                                lambda a, b: a + b
    >
    > into something like:
    >
    > Python 2                      Python 3
    > lambda (a,b),c: a + b + c     lambda t, c: t[0] + t[1] + c
    >                                lambda a, b, c: a + b + c
    >
    > it is unclear at first sight that it refers to tuple argument unpacking.


    Your proposed example is clearer in some respects, but mine is more
    minimal. And I think that anyone who _thinks_ about mine will get the
    point. (The document is short, but I never claimed it was a quick
    read;-)

    > There should also some mention that tuple argument unpacking for regular
    > function (def) is also gone.


    I probably should have, but it is hard to fit any more in... esp.
    since I don't want to take anything out.

    > Also, I'm not sure what this change is referring to:
    > Python 2                 Python 3
    > L = list(seq)            L = sorted(seq)
    > L.sort()
    >
    > L.sort is still available in python, and sorted() have been available
    > since python 2. Both list.sort() and sorted() are for different purpose,
    > and neither will be deprecated. What's the change here?


    The document is about idioms as well as changes. In this case both
    approaches work in both versions, but it seems that there are still a
    lot of people who don't know about sorted(), so I put it in to show it
    as an idiom.
    Mark Summerfield, Dec 2, 2009
    #9
  10. On 1 Dec, 21:55, Terry Reedy <> wrote:
    > Mark Summerfield wrote:
    > > I've produced a 4 page document that provides a very concise summary
    > > of Python 2<->3 differences plus the most commonly used new Python 3
    > > features. It is aimed at existing Python 2 programmers who want to
    > > start writing Python 3 programs and want to use Python 3 idioms rather
    > > than those from Python 2 where the idioms differ.

    >
    > > It uses Python 3.1 syntax since that looks like being the standard for
    > > a few years in view of the language moratorium.

    >
    > > The document is U.S. Letter size but will also print fine on A4
    > > printers.

    >
    > > It is available as a free PDF download (no registration or anything)
    > > from InformIT's website. Here's the direct link:
    > >http://ptgmedia.pearsoncmg.com/imprint_downloads/informit/promotions/...

    >
    > > And of course, if you want more on Python 3, there's always the
    > > documentation---or my book:)
    > > "Programming in Python 3 (Second Edition)" ISBN-10: 0321680561.

    >
    > What might be even *more* helpful, with contributions from others
    > perhaps, would be an indication of which changes are handled
    > automatically by 2to3.py and which must be done by hand.
    >
    > tjr


    No, that's exactly what I did not want to cover and the document says
    so up front. It is aimed at people who want Python 3 to come from
    their own brains and fingers!

    Also, the kind of info you're talking about is covered elsewhere, for
    example:
    http://diveintopython3.org/porting-code-to-python-3-with-2to3.html
    Mark Summerfield, Dec 2, 2009
    #10
  11. On 1 Dec, 23:52, John Bokma <> wrote:
    > Mark Summerfield <> writes:
    > > It is available as a free PDF download (no registration or anything)
    > > from InformIT's website. Here's the direct link:
    > >http://ptgmedia.pearsoncmg.com/imprint_downloads/informit/promotions/...

    >
    > Thanks!
    >
    > > And of course, if you want more on Python 3, there's always the
    > > documentation---or my book:)
    > > "Programming in Python 3 (Second Edition)" ISBN-10: 0321680561.

    >
    > Meh, second edition already? Haven't read the entire first edition
    > yet. Which is IMO a good book (I also gave it to my brother as a
    > present).


    If it is any consolation, the second edition should have a much longer
    life, now that we have the language moratorium. (I _really_ wanted to
    cover 3.1.)

    > Only negative point (to me) so far is that in the beginning (p8-9) the
    > book mentions placing Python programs in C:\py3eg which gives me the
    > unpleasant feeling that someone is coding on Windows XP with
    > Administrator rights...


    OK, you got me there, I only use Windows for testing purposes and my
    personal logon account does have Administrator rights, which I assumed
    was standard for personal machines? Also, the path is short. It is
    only a suggestion, it really doesn't matter where you unpack the
    examples.

    > Anyway, will add the 2nd edition to my wish list and donate the current
    > one to the library in Xalapa (USBI)  if they want it :)
    >
    > John
    Mark Summerfield, Dec 2, 2009
    #11
  12. On Dec 2, 8:01 am, Mark Summerfield <> wrote:
    > On 1 Dec, 17:50, Mark Dickinson <> wrote:
    > > My only quibble is with the statement on the first page that
    > > the 'String % operator is deprecated'.  I'm not sure that's
    > > true, for all values of 'deprecated'.  There don't appear
    > > to be any definite plans for getting rid of it just yet.

    >
    > I didn't make this up:)


    No, I didn't imagine for a second that you had!

    > According to http://docs.python.org/dev/3.0/whatsnew/3.0.html
    > "The plan is to eventually make this the only API for string
    > formatting, and to start deprecating the % operator in Python 3.1."


    I think that's a doc bug. "The plan is to ..." should read: "The plan
    was
    originally to ...". (Well, at least in the 3.1 version of the
    "what's new in 3.0" documentation; the 3.0 version that you linked to
    isn't even autogenerated any more, AFAIK, so fixes to the ReST source
    for that file never make it to the web site.)

    I'm a little confused myself about what's actually happening with
    % formatting, but here's a fairly recent python-dev posting from
    the BDFL:

    http://mail.python.org/pipermail/python-dev/2009-September/092399.html

    Mark
    Mark Dickinson, Dec 2, 2009
    #12
  13. On Wed, Dec 02, 2009 at 00:10 -0800, Mark Summerfield wrote:
    > On 1 Dec, 18:30, Lie Ryan <> wrote:


    > > Also, I'm not sure what this change is referring to:
    > > Python 2                 Python 3
    > > L = list(seq)            L = sorted(seq)
    > > L.sort()
    > >
    > > L.sort is still available in python, and sorted() have been available
    > > since python 2. Both list.sort() and sorted() are for different purpose,
    > > and neither will be deprecated. What's the change here?

    >
    > The document is about idioms as well as changes. In this case both
    > approaches work in both versions, but it seems that there are still a
    > lot of people who don't know about sorted(), so I put it in to show it
    > as an idiom.


    It would be quite nice if you could mark all the Python 3 idioms that
    work in Python 2.X as well. This would allow readers that are still using
    Python 2.X and are used to the 'old way' to adapt their coding style
    accordingly. You could just add a little (2.X) after the idiom for
    example.

    And thanks for the nice cheat sheet! :-D

    --
    .''`. Wolodja Wentland <-heidelberg.de>
    : :' :
    `. `'` 4096R/CAF14EFC
    `- 081C B7CD FF04 2BA9 94EA 36B2 8B7F 7D30 CAF1 4EFC

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.10 (GNU/Linux)

    iQIcBAEBCAAGBQJLFk2BAAoJEIt/fTDK8U78xOwP/3wttqjXXVX2ZCPg+roWPJ8E
    /rR1kXj6SBKoHNwehBMVymQhTjaK+JdFDaPyr+mcw3Bfl1KNSJXR52XgsqOs0HA8
    7TYE2s0sll0IVY2wEXxNG2Dxz5/fLLzwEC5v2ESj9qMloqVvts/oWDCEgX3KwFKo
    zDjH8+biFdoYpio3HPi1P+NP6mPIKlZNG0Q5yNPNOv4wv4fSoMU3Z/uB8reL5F3p
    GnyZNqpp2q46DzZsDbu7kf5oDzMhyLRR9WTQtw5fON9FeGkUdIkP3puR7cM9ZNW6
    Ykdn7bPFddPknFDyN+/g+DyfjmNwMWvf+eeX4/xnvARhqlkPvp/mK2duKbk5O9ku
    arSzXeTvgyqDNwzfbD0QsXjm/JAOaxPrDjUs9ZJYVgooisjYaijtHVBeQE0FnMlM
    BGBjeKn2PGBiFqQd9wIh2ucexFEzMTPhfaJzORuS188Ynk8psBbVcN24AzbON9xU
    wrpUVyQe0NRrVK5YDPyv/gZfoo6sWLIvSFm3TYh2sqEGpJb/w1QwzeJ5CakgMCJZ
    /vUg05SRvpD15bAxiZs+STuo6gbY53lrqmH7WVf/5dya0xy9SCBGGqIJ8lLHt5em
    hFlDvtb/U/GZGeb8UFpJOieRhZGnZFeXJkYWqs9kNvstOJlszhcf0CN5WT+VdhFc
    WQ60uawEDDPxaqKxHmuH
    =uX/C
    -----END PGP SIGNATURE-----
    Wolodja Wentland, Dec 2, 2009
    #13
  14. Martin P. Hellwig, Dec 2, 2009
    #14
  15. On Dec 1, 2:03 pm, Mark Summerfield <> wrote:
    > I've produced a 4 page document that provides a very concise summary
    > of Python 2<->3 differences plus the most commonly used new Python 3
    > features. It is aimed at existing Python 2 programmers who want to
    > start writing Python 3 programs and want to use Python 3 idioms rather
    > than those from Python 2 where the idioms differ.
    >
    > It uses Python 3.1 syntax since that looks like being the standard for
    > a few years in view of the language moratorium.
    >
    > The document is U.S. Letter size but will also print fine on A4
    > printers.
    >
    > It is available as a free PDF download (no registration or anything)
    > from InformIT's website. Here's the direct link:http://ptgmedia.pearsoncmg.com/imprint_downloads/informit/promotions/...
    >
    > And of course, if you want more on Python 3, there's always the
    > documentation---or my book:)
    > "Programming in Python 3 (Second Edition)" ISBN-10: 0321680561.



    I only just found out that I was supposed to give a different URL:
    http://www.informit.com/promotions/promotion.aspx?promo=137519
    This leads to a web page where you can download the document (just by
    clicking the "Download Now" button), but if you _choose_ you can also
    enter your name and email to win some sort of prize.
    Mark Summerfield, Dec 2, 2009
    #15
  16. On Dec 2, 8:53 am, Mark Dickinson <> wrote:
    > On Dec 2, 8:01 am, MarkSummerfield<> wrote:
    >
    > > On 1 Dec, 17:50, Mark Dickinson <> wrote:
    > > > My only quibble is with the statement on the first page that
    > > > the 'String % operator is deprecated'.  I'm not sure that's
    > > > true, for all values of 'deprecated'.  There don't appear
    > > > to be any definite plans for getting rid of it just yet.

    >
    > > I didn't make this up:)

    >
    > No, I didn't imagine for a second that you had!
    >
    > > According tohttp://docs.python.org/dev/3.0/whatsnew/3.0.html
    > > "The plan is to eventually make this the only API for string
    > > formatting, and to start deprecating the % operator in Python 3.1."

    >
    > I think that's a doc bug.  "The plan is to ..." should read: "The plan
    > was
    > originally to ...".  (Well, at least in the 3.1 version of the
    > "what's new in 3.0" documentation;  the 3.0 version that you linked to
    > isn't even autogenerated any more, AFAIK, so fixes to the ReST source
    > for that file never make it to the web site.)
    >
    > I'm a little confused myself about what's actually happening with
    > % formatting, but here's a fairly recent python-dev posting from
    > the BDFL:
    >
    > http://mail.python.org/pipermail/python-dev/2009-September/092399.html


    Well it seems clear to me that the BDFL wants to kill of % formatting,
    but wasn't able to for Python 3... So I still think it is reasonable
    (1) to describe it as deprecated and (2) to only teach and use
    str.format().
    Mark Summerfield, Dec 2, 2009
    #16
  17. On Dec 2, 11:20 am, Wolodja Wentland <-heidelberg.de>
    wrote:
    > On Wed, Dec 02, 2009 at 00:10 -0800, Mark Summerfield wrote:
    > > On 1 Dec, 18:30, Lie Ryan <> wrote:
    > > > Also, I'm not sure what this change is referring to:
    > > > Python 2                 Python 3
    > > > L = list(seq)            L = sorted(seq)
    > > > L.sort()

    >
    > > > L.sort is still available in python, and sorted() have been available
    > > > since python 2. Both list.sort() and sorted() are for different purpose,
    > > > and neither will be deprecated. What's the change here?

    >
    > > The document is about idioms as well as changes. In this case both
    > > approaches work in both versions, but it seems that there are still a
    > > lot of people who don't know about sorted(), so I put it in to show it
    > > as an idiom.

    >
    > It would be quite nice if you could mark all the Python 3 idioms that
    > work in Python 2.X as well. This would allow readers that are still using
    > Python 2.X and are used to the 'old way' to adapt their coding style
    > accordingly. You could just add a little (2.X) after the idiom for
    > example.


    Yes it would be nice, but it isn't quite so simple. To take sorted()
    as just one example, it was introduced in 2.4 so arguably using it
    isn't valid/idiomatic for Python 2.x programs where you care about
    backwards compatibility for the Python 2.x series... But my main
    reason for not wanting to do this is that the document is aimed at
    people who want to write Python 3, not to encourage people to stick
    with 2:)

    >
    > And thanks for the nice cheat sheet! :-D


    Thanks!

    > --
    >   .''`.     Wolodja Wentland    <-heidelberg.de>
    >  : :'  :    
    >  `. `'`     4096R/CAF14EFC
    >    `-       081C B7CD FF04 2BA9 94EA  36B2 8B7F 7D30 CAF1 4EFC
    >
    >  signature.asc
    > < 1KViewDownload
    Mark Summerfield, Dec 2, 2009
    #17
  18. On Dec 2, 11:31 am, "Martin P. Hellwig" <>
    wrote:
    > MarkSummerfieldwrote:
    >
    > <cut>> It is available as a free PDF download (no registration or anything)
    > > from InformIT's website. Here's the direct link:
    > >http://ptgmedia.pearsoncmg.com/imprint_downloads/informit/promotions/...

    >
    > <cut>
    > Very handy! Am I wrong in assuming that you forgot to include that
    > file() is gone in favour of open()?


    No you are not wrong in assuming that I forgot that:-(

    My lame excuse is that file() was introduced for isinstance() testing
    and similar, and never really as a replacement for open().

    Anyway, I have now added:

    fh = file(fname, mode) | fh = open(fname, mode)

    I've sent a new PDF with this change to InformIT, so hopefully it'll
    become available soon from
    http://www.informit.com/promotions/promotion.aspx?promo=13751
    Mark Summerfield, Dec 2, 2009
    #18
  19. On Dec 2, 4:22 pm, Mark Summerfield <> wrote:
    > On Dec 2, 11:31 am, "Martin P. Hellwig" <>
    > wrote:
    >
    > > MarkSummerfieldwrote:

    >
    > > <cut>> It is available as a free PDF download (no registration or anything)
    > > > from InformIT's website. Here's the direct link:
    > > >http://ptgmedia.pearsoncmg.com/imprint_downloads/informit/promotions/....

    >
    > > <cut>
    > > Very handy! Am I wrong in assuming that you forgot to include that
    > > file() is gone in favour of open()?

    >
    > No you are not wrong in assuming that I forgot that:-(
    >
    > My lame excuse is that file() was introduced for isinstance() testing
    > and similar, and never really as a replacement for open().
    >
    > Anyway, I have now added:
    >
    >     fh = file(fname, mode) | fh = open(fname, mode)
    >
    > I've sent a new PDF with this change to InformIT, so hopefully it'll
    > become available soon fromhttp://www.informit.com/promotions/promotion.aspx?promo=13751


    Oops wrong URL again, should have been:
    http://www.informit.com/promotions/promotion.aspx?promo=137519
    .... time to go offline and sleep ...
    Mark Summerfield, Dec 2, 2009
    #19
  20. Mark Summerfield

    John Posner Guest

    On Wed, 02 Dec 2009 10:55:23 -0500, Mark Summerfield <>
    wrote:

    > On Dec 1, 2:03 pm, Mark Summerfield <> wrote:
    >> I've produced a 4 page document that provides a very concise summary
    >> of Python 2<->3 differences plus the most commonly used new Python 3
    >> features. It is aimed at existing Python 2 programmers who want to
    >> start writing Python 3 programs and want to use Python 3 idioms rather
    >> than those from Python 2 where the idioms differ.


    Mark, I add my thanks to those of the other responders. If you find space,
    you might consider adding another str.format() feature:

    Goal: place integer 456 flush-right in a field of width 8

    Py2: "%%%dd" % 8 % 456
    Py3: "{0:{1}d}".format(456, 8)

    With str.format(), you don't need to nest one formatting operation within
    another. A little less mind-bending, and every little bit helps!

    -John
    John Posner, Dec 2, 2009
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    1,198
    IchBin
    Mar 25, 2006
  2. David Goodger

    reStructuredText Cheat Sheet

    David Goodger, Jun 1, 2004, in forum: Python
    Replies:
    2
    Views:
    584
    David Goodger
    Jun 3, 2004
  3. Riccardo T.

    Cheat sheet

    Riccardo T., Dec 27, 2007, in forum: Python
    Replies:
    22
    Views:
    785
    Riccardo T.
    Jan 1, 2008
  4. Martin Rinehart

    5-Language Cheat Sheet

    Martin Rinehart, Jul 19, 2010, in forum: C++
    Replies:
    3
    Views:
    366
    Martin Rinehart
    Jul 21, 2010
  5. Tim Hunter

    Windows new user cheat sheet?

    Tim Hunter, Apr 29, 2007, in forum: Ruby
    Replies:
    9
    Views:
    144
    Richard Conroy
    Nov 6, 2007
Loading...

Share This Page