I don't see a way to achieve it using a liquid template. Logic app liquid leverages the opensource dotliquid where you can also contribute. Looking into the source code I don't see a way to achieve your result using DotLiquid as it is a limitation from their end.
Alternately, to convert the string to your desire format you can try to use inline code by writing your own code to achieve the desired result.
Please 'Accept as answer' and ‘Upvote’ if it helped so that it can help others in the community looking for help on similar topics.