How to insert string into a string in specific position with Python

2 Answers

0 votes
s = 'abc def ghi jkl'

index = s.find('ghi')

s = s[:index] + 'XYZ ' + s[index:]

print(s)

 
    
 
'''
run:
 
abc def XYZ ghi jkl

'''

 



answered Apr 14, 2021 by avibootz
0 votes
s = 'abc def ghi jkl'

lst = s.split()

lst.insert(2, 'XYZ')

s = ' '.join(lst)

print(s)

 
    
 
'''
run:
 
abc def XYZ ghi jkl

'''

 



answered Apr 14, 2021 by avibootz

Related questions

1 answer 239 views
2 answers 261 views
1 answer 203 views
1 answer 179 views
1 answer 189 views
1 answer 239 views
...