0
How to replace word in a string in Python without using replace function?
2 Respostas
+ 6
The normal procedure in SL is that if you ask for help from the community, you first should go as far as you can, then publish your code here. Is there anything you can show?
You can use several technical approaches, but I would recommend 2 common ones:
#Using string
- Use find() to get position of word to replace in string
- Use string with slices
- Add new word
- Add the rest of the string
# Using list
- Split string to a list of words
- Find index of string to replace
- Replace old word by new word
- put list back to a string with join()
+ 4
Have look at the string and list methods:
https://www.w3schools.com/JUMP_LINK__&&__python__&&__JUMP_LINK/python_ref_string.asp
https://www.w3schools.com/python/python_ref_list.asp