1

I need to generate a random IP address that starts with "192.168.0." so basically I just need to generate a random number between 0 and 255 and add to the end of this string, but the problem is that I don't know how to merge a integer and a string.

That's my code so far:

import random

x=random.randint(0,255)
randIP = '192.168.0.'
randIP.append(x)
print randIP

3 Answers 3

4

In general, don't use operator.add (e.g. +) to deal with strings. There's nothing particularly wrong with it, but it's good practice to use string formatting instead.

Try this instead:

last_octet = random.randint(0,255) # please use descriptive var names
IP_format = "{}.{}.{}.{}"
IP = IP_format.format(192,168,0,last_octet)

Your approach won't work because you're using append on a string. append is (generally considered to be) a method for a list to add to itself. If you'd like to use that method, you could do:

last_octet = random.randint(0,255)
IP_list = [192,168,0]
IP_list.append(last_octet) # IP_list is now [192,168,0,<random number>]
IP = ".".join(map(str,IP_list))

In that last line I use a combination of two functions you likely don't know about.

  • map takes two arguments, the first is a function (in this case str, e.g. str(192) --> "192") and the second is an iterable (in this case the list IP_list, but iterables are anything you can use a for loop on). It applies the function you give it to every element in the iterable, and leaves you with a DIFFERENT iterable in Python3 called a map object. In Python2, it returned a list.

  • ".".join is explicitly calling str.join. It accepts an iterable as an argument (see above), and puts the str between each element of the iterable. ".".join(["192","168","0","255"]) gives you "192.168.0.255". This is possibly most often used by its alias in os.path, os.path.join which takes a set of arguments and puts the local path separator between each one, e.g. os.path.join("C:","Program Files","AdamSmith","locale","en-US","docs.html") would give you a string on Windows that's "C:\Program Files\AdamSmith\locale\en-US\docs.html" and on *nix that's C:/Program Files/AdamSmith/locale/en-US/docs.html"

Sign up to request clarification or add additional context in comments.

Comments

1

You can use the += operator for appending, but you'll need to convert x to a string first using str:

import random

x=random.randint(0,255)
randIP = '192.168.0.'
randIP += str(x)
print randIP

Comments

1

Try different approach: formatting. It may look like that:

import random

x=random.randint(0,255)
randIP = '192.168.0.%s' % x
print randIP

7 Comments

If it's an int, use %d, not %s. (Not my downvote, though.)
string interpolation is not recommended since str.format provides the same benefits with greater functionality.
@AdamSmith Just because it provides greater functionality in general does not make it better in a simple case like this where the greater functionality is not required at all.
@AdamSmith Fair point. Though to address your last question, for one thing string interpolation has shorter syntax and can thus be more readable in the simple cases. This being a simple case, I'd still prefer it over .format. It's also not objectively worse in this case because the results are equivalent.
@DaniëlKnippers I can respect that. I still prefer if type(item) is list over isinstance(item,list) for the same reasons, even though it is objectively worse in ALL cases :).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.