Without Regex; str methods (str.partition and str.rpartition):
In [185]: filename = mysting.rpartition('/')[-1]
In [186]: filename
Out[186]: 'happy (463).jpg'
In [187]: f"{filename.partition(' ')[0]}.{filename.rpartition('.')[-1]}"
Out[187]: 'happy.jpg'
With Regex; re.sub:
re.sub(r'.*/(?!.*/)([^\s]+)[^.]+(\..*)', r'\1\2', mysting)
.*/ greedily matches upto last /
The zero-width negative lookahead (?!.*/) ensures there is no / in anyplace forward
([^\s]+) matches upto the next whitespace and put as the first captured group
[^.]+ matches upto next .
(\..*) matches a literal . followed by any number of characters and put as the second captured group; if you want to match more conservatively like 3 characters or even literal .jpg you can do that also
in the replacement, only the captured groups are used
Example:
In [183]: mysting = '/content/drive/My Drive/data/happy (463).jpg'
In [184]: re.sub(r'.*/(?!.*/)([^\s]+)[^.]+(\..*)', r'\1\2', mysting)
Out[184]: 'happy.jpg'
'/content/drive/My Drive/data/happy (463).jpg'.split("/")[-1]if you don't want to use regex. Or better,'/content/drive/My Drive/data/happy (463).jpg'.split(os.sep)[-1]