Slicing

def insertStringAtPosition(inputStr, strToInsert, pos):
    return inputStr[:pos] + strToInsert + inputStr[pos:]


result = insertStringAtPosition('1234567', '<-->', 3)
print(result)  # 123<-->4567

result = insertStringAtPosition('1234567', '-->', 10)
print(result)  # 1234567-->

Leave a Comment

Your email address will not be published. Required fields are marked *