Asdfg Asdfg - 1 year ago 91
Python Question

Python - read everything after second or third last dot

I need to get the below output from the input.

Expected output: city.state

Expected output: city.state

This is the expression i have that works when the Input does not have . at the end:


How do i modify this expression so that
has the same output as

Answer Source
input_ = ''
x = [a for a in input_.split('.') if a]

print('.'.join(x[-2:])) # prints city.state

Works with both input.