I want to add '-' after every char in string except at start and end.
Example- Input - 'abcd' Expected Output - 'a-b-c-d' I tried -
Str1 = re.sub(r'([a-z])([a-z])',r'\1-\2',Str1)
What am I doing wrong?
Using string join is probably the best option here, but if you wanted to stick with your regex approach, here is one way to do it:
inp = "abcd"
output = re.sub(r'([a-z])(?!$)', '\\1-', inp)
print(output)
This prints:
a-b-c-d
The idea here is to capture each letter and replace with that letter, followed by a dash, if the letter be not the final letter in the string.