Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python replace nth occurrence in string

my_string = "This is a long string that contain a string about a string"

# I want to replace the second "string" with "text"

nth = 2  

my_string = my_string.replace("string", "text", nth)
my_string = my_string.replace("text", "string", nth-1)
# >>> my_string == "This is a long string that contain a text about string"
Comment

PREVIOUS NEXT
Code Example
Python :: connect to spark cluster 
Python :: How to take total count of words in the list python 
Python :: run django localhost server 
Python :: python function get number of arguments 
Python :: remove element from dictionary python 
Python :: check python version windows 
Python :: ping from python 
Python :: iterate backwards through a list python 
Python :: install python in docker file 
Python :: intersection() Function of sets in python 
Python :: python bar plot groupby 
Python :: requests 
Python :: How to convert string date to datetime format in python 
Python :: python compare timestamps 
Python :: pandas dataframe column based on another column 
Python :: how do i limit decimals to only two decimals in python 
Python :: tkinter button hide 
Python :: yaxis on the right matplotlib 
Python :: python script to copy files to remote server 
Python :: progress bar python 
Python :: django in conda 
Python :: ppcm python 
Python :: python 3d array 
Python :: django include 
Python :: python split string size 
Python :: split a string into N equal parts. 
Python :: python align label left 
Python :: Find All Occurrences of a Substring in a String in Python 
Python :: disable close button in tkinter 
Python :: print schema in pandas dataframe 
ADD CONTENT
Topic
Content
Source link
Name
4+6 =