Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2005
    Posts
    62
    Rep Power
    9

    How to turn a number into a string?


    Suppose I have a 3 and 4 in type long, then I would want to turn them into "3" or "4", How can I do that?
    As ussual, if I found out, i'll post.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2005
    Posts
    62
    Rep Power
    9
    Well, I did find out:
    numer = 4
    string = "%s" % (number,)
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    51
    Hi!

    And what about
    Code:
    number = 4
    string = str(number)


    Regards, mawe
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    11
    ya the other way would work but you would have to replace %s with %i and remove the comma at the end
    Those people who think they know everything are a great annoyance to those of us who do.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2005
    Posts
    62
    Rep Power
    9
    Thanks both.
    Well, I did use my method with no issues...
    >>> a = 4
    >>> string = "%s" % (a,)
    >>> type(string)
    <type 'str'>
    But mawes seems better... thanks again!
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    51
    Originally Posted by monkeyman23555
    ya the other way would work but you would have to replace %s with %i and remove the comma at the end
    %s turns everything to a string
    Code:
    >>> "%s" % 4
    '4'
    >>> "%s" % 4.4
    '4.4'
  12. #7
  13. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    There is also another, faster way to do this kind of thing: Repr() and it's syntactic alias the back-quote.

    Code:
    >>> str(1)
    '1'
    >>> repr(1)
    '1'
    >>> `1`
    '1'
    >>>
    Take care,

    Mark.

    Comments on this post

    • CyBerHigh agrees
    programming language development: www.netytan.com Hula

  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    Originally Posted by monkeyman23555
    ya the other way would work but you would have to replace %s with %i and remove the comma at the end
    thats only in c or lower languages of the sort. in python its dynamic enough to not require that sort of thing.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    51
    Originally Posted by netytan
    Repr() and it's syntactic alias the back-quote.
    For integers yes, but this
    Code:
    >>> repr(1.9)
    '1.8999999999999999'
    may not always be what you want
  18. #10
  19. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    If the number doesn't really matter then you can use round().
    Code:
    >>> `round(5.5)`
    '6.0'
    Or
    Code:
    >>> `int(round(5.5))`
    '6'
    Or to keep the float and make it a string:
    Code:
    >>> `float(5.5)`
    '5.5'
    Last edited by †Yegg†; August 24th, 2005 at 08:07 AM.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    51
    And that's easier than str(5.5)?
  22. #12
  23. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    I think that we started looking for what was faster.
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    51
    Code:
    $ python -m timeit 'str(5.5)'
    100000 loops, best of 3: 3.12 usec per loop
    $ python -m timeit 'repr(float(5.5))'
    100000 loops, best of 3: 3.66 usec per loop
  26. #14
  27. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Hmm. Where are you accessing that timeit module from?
    Also, not repr(), try it with ``.
  28. #15
  29. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    610
    Rep Power
    65

    Smile


    Originally Posted by danf_1979
    Well, I did find out:
    numer = 4
    string = "%s" % (number,)
    You can use %s to represent a number, the comma after number is not needed, but you have to match the name of your variable (numer is not the same as number). Actually if you look at (number,) as a tuple you are correct!
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo