filename = "input.txt" s = 0 with open(filename, 'r') as f: for line in f.readlines(): mot=[] for car in line: if car.isdigit(): mot.append(car) s+=int( "".join([mot[0], mot[-1]]) ) print(s)