Python 将输出追加到字符串
如何将Python 将输出追加到字符串,python,python-3.x,Python,Python 3.x,如何将ip的输出附加到字符串 import ipaddress import random def main(): for _ in range(10000): ip = (ipaddress.IPv4Address(random.randint(0,2 ** 32))) print(ip) main() 对字符串列表使用str的join方法 import ipaddress import random acc = [] def ma
ip
的输出附加到字符串
import ipaddress
import random
def main():
for _ in range(10000):
ip = (ipaddress.IPv4Address(random.randint(0,2 ** 32)))
print(ip)
main()
对字符串列表使用
str
的join
方法
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
使用
str.join
的简单解决方案使用理解:
', '.join(ipaddress.IPv4Address(random.randint(0,2 ** 32)) for _ in range(10000))
您认为应该如何将ip的输出附加到字符串中?