Insert String Into Another String at Given Position using Python

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

Cancel reply

Your email address will not be published.