import re s = "Hello, World! This is a reallly long string" print(re.findall(r'^\S.{0,18}\S\b|\b\S.{0,18}\S$', s))
Standard input is empty
['Hello, World', 'reallly long string']
The brand new service which powers Ideone!
Widget for compiling and running the source code in a web browser!