converting an integer to a string

Discussion in 'Python' started by Ken Fettig, Aug 6, 2003.

  1. Ken Fettig

    Ken Fettig Guest

    I have a quick simple question. How do you convert an integer to a string in
    Python?

    Thanks

    Ken Fettig
    mailto:
     
    Ken Fettig, Aug 6, 2003
    #1
    1. Advertising

  2. Ken Fettig

    Sean Ross Guest

    "Ken Fettig" <> wrote in message
    news:...
    > I have a quick simple question. How do you convert an integer to a string

    in
    > Python?



    >>> 1

    1
    >>> str(1)

    '1'
    >>>


    HTH,
    Sean
     
    Sean Ross, Aug 6, 2003
    #2
    1. Advertising

  3. Ken> I have a quick simple question. How do you convert an integer to a
    Ken> string in Python?

    Take your pick:

    str(someint)
    repr(someint)
    `someint`
    '%d' % someint

    Which is most appropriate may well depend on your tastes and your context.
    `someint` is just syntactic sugar for repr(someint), and is falling out of
    favor with many people. In the case of integers, str(someint) and
    repr(someint) are the same, so your choice there is a tossup unless you are
    str()'ing or repr()'ing other objects as well (str() generally tries to be
    "readable", repr() generally tries to be "parseable"). For most types
    repr() and str() generate different output. The experiment is probably
    educational enough to perform once, so I won't go into detail.

    The %-format version is appropriate if you want to embed it into a larger
    string, e.g.:

    '%s is %d years old' % (person, age)

    Don't forget the dict form as well:

    '%(name)s is %(age)d years old' % locals()

    Skip
     
    Skip Montanaro, Aug 6, 2003
    #3
  4. Ken Fettig

    John Roth Guest

    "Skip Montanaro" <> wrote in message
    news:...
    >
    > Ken> I have a quick simple question. How do you convert an integer to

    a
    > Ken> string in Python?
    >
    > Take your pick:
    >
    > str(someint)
    > repr(someint)
    > `someint`
    > '%d' % someint
    >
    > Which is most appropriate may well depend on your tastes and your context.
    > `someint` is just syntactic sugar for repr(someint), and is falling out of
    > favor with many people. In the case of integers, str(someint) and
    > repr(someint) are the same, so your choice there is a tossup unless you

    are
    > str()'ing or repr()'ing other objects as well (str() generally tries to be
    > "readable", repr() generally tries to be "parseable"). For most types
    > repr() and str() generate different output. The experiment is probably
    > educational enough to perform once, so I won't go into detail.
    >
    > The %-format version is appropriate if you want to embed it into a larger
    > string, e.g.:
    >
    > '%s is %d years old' % (person, age)
    >
    > Don't forget the dict form as well:
    >
    > '%(name)s is %(age)d years old' % locals()


    The % format also allows you some formatting options that the others
    don't.

    John Roth
    >
    > Skip
    >
     
    John Roth, Aug 6, 2003
    #4
    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. claire.bell1

    Converting an integer to a string.

    claire.bell1, Jul 19, 2003, in forum: C++
    Replies:
    0
    Views:
    348
    claire.bell1
    Jul 19, 2003
  2. Gil
    Replies:
    13
    Views:
    1,036
    Default User
    Jan 29, 2004
  3. Alex Neumann

    Converting a string to an integer

    Alex Neumann, May 10, 2004, in forum: C++
    Replies:
    6
    Views:
    3,208
    Leor Zolman
    May 11, 2004
  4. Replies:
    5
    Views:
    5,598
    Shani
    Mar 19, 2005
  5. Randy Kramer
    Replies:
    12
    Views:
    376
    Robert Klemme
    Oct 25, 2007
Loading...

Share This Page