-1
s = "AMCDEFGHI"
s.replace('A','X')
print(s)

Output: "AMCDEFGHI"

Could anyone please let me know if developers have extracted this functionality.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • 1
    Duplicate: [String replace doesn't appear to be working](https://stackoverflow.com/questions/26943256/string-replace-doesnt-appear-to-be-working) – wjandrea Apr 10 '20 at 04:15

4 Answers4

1

Strings are immutable, you need to assign the value return by replace to new string

s = "AMCDEFGHI"
s = s.replace('A','X')
print(s)
Nandu Raj
  • 2,072
  • 9
  • 20
0

Strings in Python are immutable, so you will have to reassign s to the replace call in order for it to stick:

s = "AMCDEFGHI"
s = s.replace('A','X')
print(s)

This prints:

XMCDEFGHI
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

use this instead

    s = "AMCDEFGHI"
    s=s.replace('A','X')
    print(s)
Ayush
  • 13
  • 5
0

you need to assign it to s

s = "AMCDEFGHI"
s=s.replace('A','X')
print(s)

result is XMCDEFGHI
Gerry P
  • 7,662
  • 3
  • 10
  • 20