Python

Page content

Python Snippets

Zfill

Padding Zero

>>> for i in range(1,10):
...     str(i).zfill(4)
'0001'
'0002'
'0003'
...

Different Padding

#!/usr/bin/env python3
for i in range(1,99):
  print( str(i).zfill(4) +" "+ str(i).rjust(4, '-') +" "+ str(i).ljust(4, '-') +" "+ str(i).center(4, '-') )

0001 ---1 1--- -1--
0002 ---2 2--- -2--
0003 ---3 3--- -3--
...

Padding with Function

#!/usr/bin/env python3

len=8
pad='-'

def my_print(i,len=4,pad='_'):
  print( str(i).zfill(len) +" "+ str(i).rjust(len, pad) +" "+ str(i).center(len, pad) +" "+ str(i).ljust(len, pad) )

for i in range(8,12):
  my_print(i,len,pad)

for i in range(98,102):
  my_print(i,len,pad)

for i in range(998,1002):
  my_print(i,len,pad)

00000008 -------8 ---8---- 8-------
00000009 -------9 ---9---- 9-------
00000010 ------10 ---10--- 10------
00000011 ------11 ---11--- 11------
00000098 ------98 ---98--- 98------
00000099 ------99 ---99--- 99------
00000100 -----100 --100--- 100-----
00000101 -----101 --101--- 101-----
00000998 -----998 --998--- 998-----
00000999 -----999 --999--- 999-----
00001000 ----1000 --1000-- 1000----
00001001 ----1001 --1001-- 1001----

DNS Lookup

python3 -c 'import socket; print(socket.gethostbyname("www.stoege.net"))'
116.203.179.238

Reverse Lookup

python3 -c 'import socket; print(socket.gethostbyaddr("116.203.179.238"))'
('static.238.179.203.116.clients.your-server.de', [], ['116.203.179.238'])
import dns.reversename
n = dns.reversename.from_address("116.203.179.238")
print(n)
print(dns.reversename.to_address(n))

238.179.203.116.in-addr.arpa.
116.203.179.238

sha256: 575bfb5d2c632dc93e7e8e9412655e94ea61e1eaceedc001395339151ca0ac4b