Reputation: 11
How can i append the output of ip
to a string
import ipaddress
import random
def main():
for _ in range(10000):
ip = (ipaddress.IPv4Address(random.randint(0,2 ** 32)))
print(ip)
main()
Upvotes: 0
Views: 74
Reputation: 27495
A simple solution using str.join
using a comprehension:
', '.join(ipaddress.IPv4Address(random.randint(0,2 ** 32)) for _ in range(10000))
Upvotes: 0
Reputation: 1227
Use the join
method of str
on a list of strings.
import ipaddress
import random
acc = []
def main():
for _ in range(10000):
ip = (ipaddress.IPv4Address(random.randint(0,2 ** 32)))
print(ip)
# append to a list instead of printing
acc.append(str(ip)) # cast the ip to a string
main()
print(" ".join(acc)) # using space as separator
Upvotes: 1