import os
ip = os.popen('"ip -4 -br addr show eth0 | grep -E -o '([0-9]{1,3}\.){3}[0-9]{1,3}'"').read().strip()
print(ip)
#Or using variable to define network interface card
nic = "eth0"
ip = os.popen('"ip -4 -br addr show " + nic + " | grep -E -o '([0-9]{1,3}\.){3}[0-9]{1,3}'"').read().strip()
print(ip)
Extend regular expression (-E)
Print only the matched parts (-o)
([0-9]{1,3}\.){3}
[0-9] -> Digits 0 to 9.
{1,3} -> Minimum 1 digit, maximum 3 digits.
\. Adds a '.', the '\' works as a escape character.
= {3} Repeats three times.
[0-9]{1,3} Same as part 1, no '.', no repeat.