convert ints in a range to strings

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • hokieghal99

    convert ints in a range to strings

    Hi,

    I'm trying to do this:

    ------------------------------
    a="192."
    b="168."
    c="1."
    r = range(256)
    for r in r:
    print a + b + c + r
    ------------------------------

    But, I get this error: cannot concatenate 'str' and 'int' objects. So, I
    need to convert the ints in the range to strs, but I do not know how to
    do that. Could someone help me? Ultimately, I want to print out
    something like this to a text file:

    192.168.1.0
    192.168.1.1
    192.168.1.2
    ....
    192.168.1.255

    Thanks!!!!




  • Duncan Smith

    #2
    Re: convert ints in a range to strings


    "hokieghal9 9" <hokiegal99@hot mail.com> wrote in message
    news:bkap6g$4o1 [email protected] t.edu...[color=blue]
    > Hi,
    >
    > I'm trying to do this:
    >
    > ------------------------------
    > a="192."
    > b="168."
    > c="1."
    > r = range(256)
    > for r in r:
    > print a + b + c + r
    > ------------------------------
    >
    > But, I get this error: cannot concatenate 'str' and 'int' objects. So, I
    > need to convert the ints in the range to strs, but I do not know how to
    > do that. Could someone help me? Ultimately, I want to print out
    > something like this to a text file:
    >
    > 192.168.1.0
    > 192.168.1.1
    > 192.168.1.2
    > ...
    > 192.168.1.255
    >
    > Thanks!!!!
    >
    >
    >
    >[/color]
    [color=blue][color=green][color=darkred]
    >>> a="192."
    >>> b="168."
    >>> c="1."
    >>> r = 12
    >>> a + b + c + str(r)[/color][/color][/color]
    '192.168.1.12'[color=blue][color=green][color=darkred]
    >>> '%s%s%s%d' % (a,b,c,r)[/color][/color][/color]
    '192.168.1.12'[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    I believe the second approach is generally faster.

    Duncan


    Comment

    • Paul Rubin

      #3
      Re: convert ints in a range to strings

      hokieghal99 <hokiegal99@hot mail.com> writes:[color=blue]
      > ------------------------------
      > a="192."
      > b="168."
      > c="1."
      > r = range(256)
      > for r in r:
      > print a + b + c + r[/color]

      for r in range(256):
      print "192.168.1. %d" % r

      Comment

      • hokiegal99

        #4
        Re: convert ints in a range to strings

        Thank you (Paul & Duncan) for showing me how to do this!

        Paul Rubin wrote:[color=blue]
        > hokieghal99 <hokiegal99@hot mail.com> writes:
        >[color=green]
        >>------------------------------
        >>a="192."
        >>b="168."
        >>c="1."
        >>r = range(256)
        >>for r in r:
        >> print a + b + c + r[/color]
        >
        >
        > for r in range(256):
        > print "192.168.1. %d" % r[/color]


        Comment

        • Heather Coppersmith

          #5
          Re: convert ints in a range to strings

          On Wed, 17 Sep 2003 19:02:40 -0400,
          hokieghal99 <hokiegal99@hot mail.com> wrote:
          [color=blue]
          > Hi,
          > I'm trying to do this:[/color]
          [color=blue]
          > ------------------------------
          > a="192."
          > b="168."
          > c="1."
          > r = range(256)
          > for r in r:
          > print a + b + c + r
          > ------------------------------[/color]
          [color=blue]
          > But, I get this error: cannot concatenate 'str' and 'int' objects. So, I
          > need to convert the ints in the range to strs, but I do not know how to
          > do that. Could someone help me? Ultimately, I want to print out
          > something like this to a text file:[/color]
          [color=blue]
          > 192.168.1.0
          > 192.168.1.1
          > 192.168.1.2
          > ...
          > 192.168.1.255[/color]

          Try any or all of these:

          print a + b + c + str( r )

          print a + b + c + "%d" % r

          print a + b + c + "%s" % r

          print "%s%s%s%d" % (a, b, c, r)

          Regards,
          Heather

          --
          Heather Coppersmith
          That's not right; that's not even wrong. -- Wolfgang Pauli

          Comment

          • hokiegal99

            #6
            Re: convert ints in a range to strings

            Thanks Heather... this did it:

            outputFile = file('ips.txt', 'w')
            r = range(256)
            for r in range(256):
            f = '192.168.1.%s\n ' % r #Change this line to macth your network.
            outputFile.writ e(f)
            outputFile.clos e()

            Heather Coppersmith wrote:[color=blue]
            > Try any or all of these:
            >
            > print a + b + c + str( r )
            >
            > print a + b + c + "%d" % r
            >
            > print a + b + c + "%s" % r
            >
            > print "%s%s%s%d" % (a, b, c, r)
            >
            > Regards,
            > Heather
            >[/color]


            Comment

            • Michael Sweeney

              #7
              Re: convert ints in a range to strings

              > Hi,[color=blue]
              >
              > I'm trying to do this:
              >
              > ------------------------------
              > a="192."
              > b="168."
              > c="1."
              > r = range(256)
              > for r in r:
              > print a + b + c + r
              > ------------------------------
              >[/color]

              You could try this:

              a="192"
              b="168"
              c="1"
              for d in range(256):
              print "%s.%s.%s.%s"%( a,b,c,d)


              192.168.1.0
              192.168.1.1
              192.168.1.2
              192.168.1.3
              ....
              192.168.1.255

              - Mike

              Comment

              • Asun Friere

                #8
                Re: convert ints in a range to strings

                hokieghal99 <hokiegal99@hot mail.com> wrote in message news:<bkap6g$4o [email protected]. vt.edu>...[color=blue]
                > Hi,
                >
                > I'm trying to do this:
                >
                > ------------------------------
                > a="192."
                > b="168."
                > c="1."
                > r = range(256)
                > for r in r:
                > print a + b + c + r
                > ------------------------------
                >
                > But, I get this error: cannot concatenate 'str' and 'int' objects. So, I
                > need to convert the ints in the range to strs, but I do not know how to
                > do that. Could someone help me? Ultimately, I want to print out
                > something like this to a text file:
                >
                > 192.168.1.0
                > 192.168.1.1
                > 192.168.1.2
                > ...
                > 192.168.1.255
                >
                > Thanks!!!![/color]

                for x in r :
                print a + b + c + str(x)


                but why not use ints all the way along like this:

                a = 192
                b = 168
                c = 1
                for n in range(256) :
                print "%d.%d.%d.% d" % (a, b, c, n)

                or simply don't use them at all:

                for n in range(256) :
                print "192.168.1. %d" % n

                Comment

                • Raymond Hettinger

                  #9
                  Re: convert ints in a range to strings

                  [hokieghal99][color=blue]
                  > a="192."
                  > b="168."
                  > c="1."
                  > r = range(256)
                  > for r in r:[/color]

                  This rebinding of r is an amazing non-error.
                  I had to read it twice before I believed that it worked.
                  Working or not, don't do this. Changing the meaning
                  of variable like this drives some people off the edge
                  and they end up working in pure functional languages
                  and never recover.
                  [color=blue]
                  > print a + b + c + r[/color]

                  As the others pointed out, str(r) will do the trick.


                  Raymond Hettinger


                  Comment

                  • Duncan Smith

                    #10
                    Re: convert ints in a range to strings


                    "hokiegal99 " <hokiegal99@hot mail.com> wrote in message
                    news:3F6900D2.7 080806@hotmail. com...[color=blue]
                    > Thanks Heather... this did it:
                    >
                    > outputFile = file('ips.txt', 'w')
                    > r = range(256)
                    > for r in range(256):
                    > f = '192.168.1.%s\n ' % r #Change this line to macth your network.
                    > outputFile.writ e(f)
                    > outputFile.clos e()
                    >[/color]

                    [snip]

                    The second line of this code does nothing useful. My feeling is that as r
                    is an integer you should really use %d, rather than %s.

                    Duncan


                    Comment

                    Working...