Python Forum
[SOLVED] How to replace characters in a string?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED] How to replace characters in a string?
#2
Strings are immutable. The replace() method creates a new string and leaves the original string unchanged.
>>> s = "spam spam <i> spam"
>>> t = s.replace("<i>", "[i]")
>>> t
'spam spam [i] spam'
>>> s
'spam spam <i> spam'
>>> 
Winfried likes this post
« We can solve any problem by introducing an extra level of indirection »
Reply


Messages In This Thread
RE: [SOLVED] How to replace characters in a string? - by Gribouillis - Sep-04-2024, 01:37 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question [SOLVED] [Beautiful Soup] Replace tag.string from another file? Winfried 2 571 May-01-2025, 03:43 PM
Last Post: Winfried
  [SOLVED] Special characters in XML ForeverNoob 3 1,988 Dec-04-2024, 01:26 PM
Last Post: ForeverNoob
  [SOLVED] Sub string not found in string ? jehoshua 4 1,561 Dec-03-2024, 09:17 PM
Last Post: jehoshua
  Need to replace a string with a file (HTML file) tester_V 1 2,039 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  doing string split with 2 or more split characters Skaperen 22 6,777 Aug-13-2023, 01:57 AM
Last Post: Skaperen
  How do I check if the first X characters of a string are numbers? FirstBornAlbratross 6 3,342 Apr-12-2023, 10:39 AM
Last Post: jefsummers
  Replace string in a nested Dictianory. SpongeB0B 2 2,551 Mar-24-2023, 05:09 PM
Last Post: SpongeB0B
  Replace with upper(string) WJSwan 7 3,014 Feb-10-2023, 10:28 AM
Last Post: WJSwan
  [SOLVED] [BeautifulSoup] Why does it turn inserted string's brackets into &lt;/&gt;? Winfried 0 2,794 Sep-03-2022, 11:21 PM
Last Post: Winfried
  [SOLVED] [BeautifulSoup] Turn select() into comma-separated string? Winfried 0 1,981 Aug-19-2022, 08:07 PM
Last Post: Winfried

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020