Home > Back-end >  Adding string to a string before some certain characters?
Adding string to a string before some certain characters?

Time:02-14

myString = '(247, 246, 244), (13, 23, 24), (85, 101, 100)'

substring = 'rgb'
  • I want to add this string the substring above, before every '(' character. So that it would be look like this below.

    myString = 'rgb(247, 246, 244), rgb(13, 23, 24), rgb(85, 101, 100)'

CodePudding user response:

Try using the .replace() method, ie myString.replace("(", "rgb(") this will just go through the string and replace every "(" with "rgb(".

CodePudding user response:

Just showing full implementation of Jasonharper's, where you change how you define your sub-string and replace the opening parenthesis:

myString = '(247, 246, 244), (13, 23, 24), (85, 101, 100)'
substring = 'rgb('
myString.replace("(", substring)
  • Related