0

I have a string, for example :

s = '000025'

now I want to replace s[:2] with some string ('ss')

  • I tried simple assignment ( s[:2] = 'ss' ) but it didn't work
  • replace method didn't work either

s.replace('00', 'ss', 1) works but I'm looking for another solution

how can I do it ?

Mazdak
  • 105,000
  • 18
  • 159
  • 188
  • 1
    Strings are immutable objects, you cannot change them in-place.You can instead do `s = 'ss' + s[2:]`. – Mazdak Apr 07 '18 at 07:48
  • 1
    Strings in python are immutable - your best bet would be to make a new string with what you want in it – Crawley Apr 07 '18 at 07:50

1 Answers1

-1

Why aren't you using s = ss + s[2:]?

Ivo
  • 3,890
  • 5
  • 22
  • 53