René's URL Explorer Experiment


Title: How to Replace a String in Python – Real Python

Open Graph Title: How to Replace a String in Python – Real Python

Description: In this tutorial, you'll learn how to remove or replace a string or substring. You'll go from the basic string method .replace() all the way up to a multi-layer regex pattern using the sub() function from Python's re module.

Open Graph Description: In this tutorial, you'll learn how to remove or replace a string or substring. You'll go from the basic string method .replace() all the way up to a multi-layer regex pattern using the sub() function from Python's re module.

Mail addresses
?subject=Python article for you&body=How to Replace a String in Python on Real Python https://realpython.com/replace-string-python/

Opengraph URL: https://realpython.com/replace-string-python/

X: @realpython

direct link

Domain: realpython.com


Hey, it has json ld scripts:
{
  "@context": "http://schema.org",
  "@type": "Article",
  "headline": "How to Replace a String in Python",
  "image": {
    "@type": "ImageObject",
    "url": "https://files.realpython.com/media/How-to-Replace-Parts-of-a-String-in-Python_Watermarked.a70c83972fa6.jpg",
    "width": 1920,
    "height": 1080
  },
  "mainEntityOfPage": {
    "@type": "WebPage",
    "@id": "https://realpython.com/replace-string-python/",
    "lastReviewed": "2025-01-15",
    "author": {
      "@type": "Person",
      "name": "Ian Currie",
      "image": "https://realpython.com/cdn-cgi/image/width=480,height=480,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/5sK42VEQ_-_Copy_-_Copy.b8a1a4a9ca1f.jpeg",
      "url": "https://realpython.com/team/icurrie/",
      "affiliation": {
        "@type": "Organization",
        "@id": "https://realpython.com/#organization",
        "name": "Real Python",
        "url": "https://realpython.com",
        "logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
      }
    },
    "reviewedBy": [
      {
        "@type": "Person",
        "name": "Aldren Santos",
        "image": "https://realpython.com/cdn-cgi/image/width=500,height=500,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/Aldren_Santos_Real_Python.6b0861d8b841.png",
        "url": "https://realpython.com/team/asantos/",
        "affiliation": {
          "@type": "Organization",
          "@id": "https://realpython.com/#organization",
          "name": "Real Python",
          "url": "https://realpython.com",
          "logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
        }
      },
      {
        "@type": "Person",
        "name": "Geir Arne Hjelle",
        "image": "https://realpython.com/cdn-cgi/image/width=800,height=800,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/gahjelle.470149ee709e.jpg",
        "url": "https://realpython.com/team/gahjelle/",
        "affiliation": {
          "@type": "Organization",
          "@id": "https://realpython.com/#organization",
          "name": "Real Python",
          "url": "https://realpython.com",
          "logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
        }
      },
      {
        "@type": "Person",
        "name": "Kate Finegan",
        "image": "https://realpython.com/cdn-cgi/image/width=400,height=400,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/VZxEtUor_400x400.7169c68e3950.jpg",
        "url": "https://realpython.com/team/kfinegan/",
        "affiliation": {
          "@type": "Organization",
          "@id": "https://realpython.com/#organization",
          "name": "Real Python",
          "url": "https://realpython.com",
          "logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
        }
      },
      {
        "@type": "Person",
        "name": "Philipp Acsany",
        "image": "https://realpython.com/cdn-cgi/image/width=400,height=400,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/phi5_2.0e61b4c66f6b.jpg",
        "url": "https://realpython.com/team/pacsany/",
        "affiliation": {
          "@type": "Organization",
          "@id": "https://realpython.com/#organization",
          "name": "Real Python",
          "url": "https://realpython.com",
          "logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
        }
      }
    ]
  },
  "datePublished": "2025-01-15T14:00:00+00:00",
  "dateModified": "2025-01-15T14:09:18.411027+00:00",
  "publisher": {
    "@type": "Organization",
    "@id": "https://realpython.com/#organization",
    "name": "Real Python",
    "url": "https://realpython.com",
    "logo": {
      "@type": "ImageObject",
      "url": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png",
      "width": 512,
      "height": 512
    },
    "description": "Real Python is a leading provider of online Python education and one of the largest language-specific online communities for software developers. It publishes high-quality learning resources, such as tutorials, books, and courses to an audience of millions of developers, data scientists, and machine learning engineers each month.",
    "slogan": "Become a Python Expert",
    "email": "info@realpython.com",
    "sameAs": [
      "https://github.com/realpython",
      "https://www.youtube.com/realpython",
      "https://twitter.com/realpython",
      "https://x.com/realpython",
      "https://www.linkedin.com/company/realpython-com/",
      "https://www.facebook.com/learnrealpython",
      "https://www.instagram.com/realpython",
      "https://www.tiktok.com/@realpython.com"
    ]
  },
  "author": {
    "@type": "Person",
    "name": "Ian Currie",
    "image": "https://realpython.com/cdn-cgi/image/width=480,height=480,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/5sK42VEQ_-_Copy_-_Copy.b8a1a4a9ca1f.jpeg",
    "url": "https://realpython.com/team/icurrie/",
    "affiliation": {
      "@type": "Organization",
      "@id": "https://realpython.com/#organization",
      "name": "Real Python",
      "url": "https://realpython.com",
      "logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
    }
  },
  "description": "In this tutorial, you'll learn how to remove or replace a string or substring. You'll go from the basic string method .replace() all the way up to a multi-layer regex pattern using the sub() function from Python's re module.",
  "hasPart": {
    "@type": "FAQPage",
    "mainEntity": [
      {
        "@type": "Question",
        "name": "How do you replace parts of a string?",
        "acceptedAnswer": {
          "@type": "Answer",
          "text": "

You replace parts of a string in Python using the .replace() method, chaining it onto the string you want to modify and providing the method with two arguments: the string you want to replace and the replacement string.

" } }, { "@type": "Question", "name": "How do you replace all occurrences in a string?", "acceptedAnswer": { "@type": "Answer", "text": "

To replace all occurrences of a substring in a string, use the .replace() method with the substring you want to replace and the new string as arguments. This method replaces all instances of the substring in the original string.

" } }, { "@type": "Question", "name": "Can you replace a string with regular expressions in Python?", "acceptedAnswer": { "@type": "Answer", "text": "

Yes, you can use the re.sub() function from the re module in Python to replace strings using regular expressions, which allows for more complex and flexible replacements.

" } }, { "@type": "Question", "name": "How do you remove part of a string?", "acceptedAnswer": { "@type": "Answer", "text": "

To remove part of a string, you can use the .replace() method, specifying the part you want to remove and using an empty string as the replacement.

" } }, { "@type": "Question", "name": "How do you replace a letter in a string?", "acceptedAnswer": { "@type": "Answer", "text": "

You replace a letter in a string by using the .replace() method, where the first argument is the letter you want to replace, and the second argument is the new letter you want to use.

" } } ] } }

authorReal Python
twitter:cardsummary_large_image
twitter:imagehttps://files.realpython.com/media/How-to-Replace-Parts-of-a-String-in-Python_Watermarked.a70c83972fa6.jpg
og:imagehttps://files.realpython.com/media/How-to-Replace-Parts-of-a-String-in-Python_Watermarked.a70c83972fa6.jpg
twitter:creator@realpython
og:typearticle

Links:

https://realpython.com/
Start Herehttps://realpython.com/start-here/
Learn Python https://realpython.com/replace-string-python/
Python Tutorials →In-depth articles and video courseshttps://realpython.com/search?kind=article&kind=course&order=newest
Learning Paths →Guided study plans for accelerated learninghttps://realpython.com/learning-paths/
Quizzes & Exercises →Check your learning progresshttps://realpython.com/quizzes/
Browse Topics →Focus on a specific area or skill levelhttps://realpython.com/tutorials/all/
Community Chat →Learn with other Pythonistashttps://realpython.com/community/
Office Hours →Live Q&A calls with Python expertshttps://realpython.com/office-hours/
Podcast →Hear what’s new in the world of Pythonhttps://realpython.com/podcasts/rpp/
Books →Round out your knowledge and learn offlinehttps://realpython.com/products/books/
Reference →Concise definitions for common Python termshttps://realpython.com/ref/
Code Mentor →BetaPersonalized code assistance & learning toolshttps://realpython.com/mentor/
Unlock All Content →https://realpython.com/account/join/
More https://realpython.com/replace-string-python/
Learner Storieshttps://realpython.com/learner-stories/
Python Newsletterhttps://realpython.com/newsletter/
Python Job Boardhttps://www.pythonjobshq.com
Meet the Teamhttps://realpython.com/team/
Become a Contributorhttps://realpython.com/jobs/
Searchhttps://realpython.com/search
https://realpython.com/search
Joinhttps://realpython.com/account/join/
Sign‑Inhttps://realpython.com/account/login/?next=%2Freplace-string-python%2F
Browse Topicshttps://realpython.com/tutorials/all/
Guided Learning Pathshttps://realpython.com/learning-paths/
Basicshttps://realpython.com/search?level=basics
Intermediatehttps://realpython.com/search?level=intermediate
Advancedhttps://realpython.com/search?level=advanced
aihttps://realpython.com/tutorials/ai/
algorithmshttps://realpython.com/tutorials/algorithms/
apihttps://realpython.com/tutorials/api/
best-practiceshttps://realpython.com/tutorials/best-practices/
careerhttps://realpython.com/tutorials/career/
communityhttps://realpython.com/tutorials/community/
databaseshttps://realpython.com/tutorials/databases/
data-sciencehttps://realpython.com/tutorials/data-science/
data-structureshttps://realpython.com/tutorials/data-structures/
data-vizhttps://realpython.com/tutorials/data-viz/
devopshttps://realpython.com/tutorials/devops/
djangohttps://realpython.com/tutorials/django/
dockerhttps://realpython.com/tutorials/docker/
editorshttps://realpython.com/tutorials/editors/
flaskhttps://realpython.com/tutorials/flask/
front-endhttps://realpython.com/tutorials/front-end/
gamedevhttps://realpython.com/tutorials/gamedev/
guihttps://realpython.com/tutorials/gui/
machine-learninghttps://realpython.com/tutorials/machine-learning/
newshttps://realpython.com/tutorials/news/
numpyhttps://realpython.com/tutorials/numpy/
projectshttps://realpython.com/tutorials/projects/
pythonhttps://realpython.com/tutorials/python/
stdlibhttps://realpython.com/tutorials/stdlib/
testinghttps://realpython.com/tutorials/testing/
toolshttps://realpython.com/tutorials/tools/
web-devhttps://realpython.com/tutorials/web-dev/
web-scrapinghttps://realpython.com/tutorials/web-scraping/
Table of Contentshttps://realpython.com/replace-string-python/#toc
How to Remove or Replace a Python String or Substringhttps://realpython.com/replace-string-python/#how-to-remove-or-replace-a-python-string-or-substring
Set Up Multiple Replacement Ruleshttps://realpython.com/replace-string-python/#set-up-multiple-replacement-rules
Leverage re.sub() to Make Complex Ruleshttps://realpython.com/replace-string-python/#leverage-resub-to-make-complex-rules
Use a Callback With re.sub() for Even More Controlhttps://realpython.com/replace-string-python/#use-a-callback-with-resub-for-even-more-control
Apply the Callback to the Scripthttps://realpython.com/replace-string-python/#apply-the-callback-to-the-script
Conclusionhttps://realpython.com/replace-string-python/#conclusion
Frequently Asked Questionshttps://realpython.com/replace-string-python/#frequently-asked-questions
https://realpython.com/feedback/survey/article/replace-string-python/liked/?from=article-sidebar
https://realpython.com/feedback/survey/article/replace-string-python/disliked/?from=article-sidebar
Replacing a String in Python 35m · 15 lessons https://realpython.com/courses/replace-string-python/
Ian Curriehttps://realpython.com/replace-string-python/#author
https://realpython.com/replace-string-python/#reader-comments
basicshttps://realpython.com/tutorials/basics/
best-practiceshttps://realpython.com/tutorials/best-practices/
stdlibhttps://realpython.com/tutorials/stdlib/
How to Remove or Replace a Python String or Substringhttps://realpython.com/replace-string-python/#how-to-remove-or-replace-a-python-string-or-substring
Set Up Multiple Replacement Ruleshttps://realpython.com/replace-string-python/#set-up-multiple-replacement-rules
Leverage re.sub() to Make Complex Ruleshttps://realpython.com/replace-string-python/#leverage-resub-to-make-complex-rules
Use a Callback With re.sub() for Even More Controlhttps://realpython.com/replace-string-python/#use-a-callback-with-resub-for-even-more-control
Apply the Callback to the Scripthttps://realpython.com/replace-string-python/#apply-the-callback-to-the-script
Conclusionhttps://realpython.com/replace-string-python/#conclusion
Frequently Asked Questionshttps://realpython.com/replace-string-python/#frequently-asked-questions
Remove adshttps://realpython.com/account/join/
Replacing a String in Pythonhttps://realpython.com/courses/replace-string-python/
personal datahttps://en.wikipedia.org/wiki/Personal_data
ISO time stampshttps://en.wikipedia.org/wiki/ISO_8601
Click here to download the free sample codehttps://realpython.com/bonus/replace-string-python-code/
https://realpython.com/replace-string-python/#how-to-remove-or-replace-a-python-string-or-substring
triple-quoted stringhttps://realpython.com/python-data-types/#triple-quoted-strings
Remove adshttps://realpython.com/account/join/
https://realpython.com/replace-string-python/#set-up-multiple-replacement-rules
list of tupleshttps://realpython.com/python-lists-tuples/
CSV filehttps://realpython.com/python-csv/
section on unpackinghttps://realpython.com/python-lists-tuples/#tuple-assignment-packing-and-unpacking
https://realpython.com/replace-string-python/#leverage-resub-to-make-complex-rules
wildcardshttps://en.wikipedia.org/wiki/Wildcard_character
regular expressionshttps://realpython.com/regex-python/
superpowershttps://xkcd.com/208/
sub() functionhttps://docs.python.org/3/library/re.html#re.sub
special characterhttps://www.regular-expressions.info/characters.html
alphanumerichttps://en.wikipedia.org/wiki/Alphanumericals
quantifierhttps://www.regular-expressions.info/refrepeat.html
character setshttps://www.regular-expressions.info/charclass.html
escapehttps://en.wikipedia.org/wiki/Escape_character
Python regex tutorialhttps://realpython.com/regex-python/
RegExrhttps://regexr.com/
Remove adshttps://realpython.com/account/join/
https://realpython.com/replace-string-python/#use-a-callback-with-resub-for-even-more-control
callback functionhttps://en.wikipedia.org/wiki/Callback_(computer_programming)
match objecthttps://docs.python.org/3/library/re.html#match-objects
https://realpython.com/replace-string-python/#apply-the-callback-to-the-script
string concatenationhttps://realpython.com/python-string-concatenation/
f-stringshttps://realpython.com/python-f-strings/
Remove adshttps://realpython.com/account/join/
https://realpython.com/replace-string-python/#conclusion
https://realpython.com/replace-string-python/#frequently-asked-questions
Click here to download the free sample codehttps://realpython.com/bonus/replace-string-python-code/
https://realpython.com/feedback/survey/article/replace-string-python/liked/?from=article-footer
https://realpython.com/feedback/survey/article/replace-string-python/disliked/?from=article-footer
Replacing a String in Pythonhttps://realpython.com/courses/replace-string-python/
https://realpython.com/team/icurrie/
» More about Ianhttps://realpython.com/team/icurrie/
https://realpython.com/team/asantos/
Aldrenhttps://realpython.com/team/asantos/
https://realpython.com/team/gahjelle/
Geir Arnehttps://realpython.com/team/gahjelle/
https://realpython.com/team/kfinegan/
Katehttps://realpython.com/team/kfinegan/
https://realpython.com/team/pacsany/
Philipphttps://realpython.com/team/pacsany/
Level Up Your Python Skills »https://realpython.com/account/join/?utm_source=rp_article_footer&utm_content=replace-string-python
Level Up Your Python Skills »https://realpython.com/account/join/?utm_source=rp_article_footer&utm_content=replace-string-python
https://realpython.com/feedback/survey/article/replace-string-python/liked/?from=article-comments
https://realpython.com/feedback/survey/article/replace-string-python/disliked/?from=article-comments
LinkedInhttps://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Frealpython.com%2Freplace-string-python%2F
Twitterhttps://twitter.com/intent/tweet/?text=Interesting%20Python%20article%20on%20%40realpython%3A%20How%20to%20Replace%20a%20String%20in%20Python&url=https%3A%2F%2Frealpython.com%2Freplace-string-python%2F
Blueskyhttps://bsky.app/intent/compose?text=Interesting%20Python%20article%20on%20%40realpython.com%3A%20How%20to%20Replace%20a%20String%20in%20Python%20https%3A%2F%2Frealpython.com%2Freplace-string-python%2F
Facebookhttps://facebook.com/sharer/sharer.php?u=https%3A%2F%2Frealpython.com%2Freplace-string-python%2F
Get tips for asking good questionshttps://realpython.com/python-beginner-tips/#tip-9-ask-good-questions
get answers to common questions in our support portalhttps://support.realpython.com
Real Python Community Chathttps://realpython.com/community/
“Office Hours” Live Q&A Sessionhttps://realpython.com/office-hours/
basicshttps://realpython.com/tutorials/basics/
best-practiceshttps://realpython.com/tutorials/best-practices/
stdlibhttps://realpython.com/tutorials/stdlib/
Replacing a String in Pythonhttps://realpython.com/courses/replace-string-python/?utm_source=realpython&utm_medium=web&utm_campaign=related-course&utm_content=replace-string-python
https://realpython.com/account/signup/?intent=continue_reading&utm_source=rp&utm_medium=web&utm_campaign=rwn&utm_content=v1&next=%2Freplace-string-python%2F
Continue »https://realpython.com/account/signup/?intent=continue_reading&utm_source=rp&utm_medium=web&utm_campaign=rwn&utm_content=v1&next=%2Freplace-string-python%2F
Sign-Inhttps://realpython.com/account/login/?next=/replace-string-python/
Start Herehttps://realpython.com/start-here/
Learning Resourceshttps://realpython.com/search
Code Mentorhttps://realpython.com/mentor/
Python Referencehttps://realpython.com/ref/
Python Cheat Sheethttps://realpython.com/cheatsheets/python/
Support Centerhttps://support.realpython.com/
Learning Pathshttps://realpython.com/learning-paths/
Quizzes & Exerciseshttps://realpython.com/quizzes/
Browse Topicshttps://realpython.com/tutorials/all/
Live Courseshttps://realpython.com/live/
Bookshttps://realpython.com/books/
Podcasthttps://realpython.com/podcasts/rpp/
Newsletterhttps://realpython.com/newsletter/
Community Chathttps://realpython.com/community/
Office Hourshttps://realpython.com/office-hours/
Learner Storieshttps://realpython.com/learner-stories/
Plans & Pricinghttps://realpython.com/account/join/
Team Planshttps://realpython.com/account/join-team/
For Businesshttps://realpython.com/account/join-team/inquiry/
For Schoolshttps://realpython.com/account/join-team/education-inquiry/
Reviewshttps://realpython.com/learner-stories/
About Ushttps://realpython.com/about/
Teamhttps://realpython.com/team/
Mission & Valueshttps://realpython.com/mission/
Editorial Guidelineshttps://realpython.com/editorial-guidelines/
Sponsorshipshttps://realpython.com/sponsorships/
Careershttps://realpython.workable.com
Press Kithttps://realpython.com/media-kit/
Merchhttps://realpython.com/merch
https://www.youtube.com/realpython
https://x.com/realpython
https://www.linkedin.com/company/realpython-com
https://www.facebook.com/LearnRealPython
https://github.com/realpython/
Privacy Policyhttps://realpython.com/privacy-policy/
Terms of Usehttps://realpython.com/terms/
Securityhttps://realpython.com/security/
Contacthttps://realpython.com/contact/
https://realpython.com/

Viewport: width=device-width, initial-scale=1, shrink-to-fit=no, viewport-fit=cover

Robots: max-image-preview:large


URLs of crawlers that visited me.