Parentheses capture. (?P<name>...) names the group so groupdict() returns a mapping.
Goal
Parse a kiosk line into city, units, and price.
import re
m = re.search(r"(\w+) (\d+)", "Nairobi 12")
print(m.group(0))
print(m.group(1), m.group(2))import re
m = re.search(r"(?P<city>\w+)\s+(?P<units>\d+)\s+(?P<price>[\d.]+)", "Mombasa 9 22.0")
print(m.groupdict())import re
line = "Kisumu 2 31.0"
city, units, price = re.search(r"(\w+) (\d+) ([\d.]+)", line).groups()
print(city, int(units), float(price))import re
text = "Nairobi 12 10.5 and Nakuru 4 22.0"
print(re.findall(r"(\w+) (\d+)", text))Pitfall
group(0) is the whole match, not the first capture. Captures start at 1.