René's URL Explorer Experiment


Title: Replace a String With .replace() (Video) – Real Python

Open Graph Title: Replace a String With .replace() – Real Python

Description: To clean up the swear words and the transcript, you need to replace the swear word with another word—or, to phrase this as an explicit task for you as a Python developer, you need to replace a string with another string. The most basic way to…

Open Graph Description: To clean up the swear words and the transcript, you need to replace the swear word with another word—or, to phrase this as an explicit task for you as a Python developer, you need to replace a string with another string. The most basic way to…

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

X: @realpython

direct link

Domain: realpython.com


Hey, it has json ld scripts:
  {
    "@context": "https://schema.org",
    "@type": "VideoObject",
    "name": "Replace a String With .replace()",
    "description": "To clean up the swear words and the transcript, you need to replace the swear word with another word—or, to phrase this as an explicit task for you as a Python developer, you need to replace a string with another string. The most basic way to…",
    "thumbnailUrl": ["https://files.realpython.com/media/How-to-Replace-Parts-of-a-String-in-Python_Watermarked.a70c83972fa6.jpg"],
    "uploadDate": "2023-08-22T14:00:00+00:00",
    "duration": "PT3M6S",
    "embedUrl": "https://player.vimeo.com/video/853867291",
    "potentialAction": {
      "@type": "SeekToAction",
      "target": "https://realpython.com/videos/replace-string-python-method/#t={seek_to_second_number}",
      "startOffset-input": "required name=seek_to_second_number"
    }
  }
  

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:typevideo.episode

Links:

https://realpython.com/
Start Herehttps://realpython.com/start-here/
Learn Python https://realpython.com/videos/replace-string-python-method/
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/videos/replace-string-python-method/
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=%2Fvideos%2Freplace-string-python-method%2F
https://realpython.com/courses/replace-string-python/#team
Replacing a String in Pythonhttps://realpython.com/courses/replace-string-python/
Philipp Acsanyhttps://realpython.com/courses/replace-string-python/#team
Recommended Tutorialhttps://realpython.com/replace-string-python/
Course Slides (.pdf)https://realpython.com/courses/replace-string-python/downloads/replace-string-python-slides/
Sample Code (.zip)https://realpython.com/courses/replace-string-python/downloads/replace-string-python-code/
Ask a Questionhttps://realpython.com/videos/replace-string-python-method/#discussion
https://realpython.com/feedback/survey/course/replace-string-python/liked/?from=lesson-title
https://realpython.com/feedback/survey/course/replace-string-python/disliked/?from=lesson-title
Transcripthttps://realpython.com/videos/replace-string-python-method/#transcript
Discussionhttps://realpython.com/videos/replace-string-python-method/#discussion
00:00https://realpython.com/videos/replace-string-python-method/#t=0.62
To clean up the swear words and the transcript,https://realpython.com/videos/replace-string-python-method/#t=0.62
you need to replace the swear word with another word—or, to phrase this ashttps://realpython.com/videos/replace-string-python-method/#t=2.98
an explicit task for you as a Python developer,https://realpython.com/videos/replace-string-python-method/#t=7.82
you need to replace a string with another string.https://realpython.com/videos/replace-string-python-method/#t=10.94
00:15https://realpython.com/videos/replace-string-python-method/#t=15.02
The most basic way to replace a string in Python is to use the .replace() stringhttps://realpython.com/videos/replace-string-python-method/#t=15.02
method. You can call the .replace() method on any string,https://realpython.com/videos/replace-string-python-method/#t=19.72
and it takes at least two arguments.https://realpython.com/videos/replace-string-python-method/#t=23.62
00:26https://realpython.com/videos/replace-string-python-method/#t=26.34
The first argument is the old string that you want to replace,https://realpython.com/videos/replace-string-python-method/#t=26.34
and the second is the replacement. You can optionally provide a third argument,https://realpython.com/videos/replace-string-python-method/#t=29.78
which is the number of replacements you want to make.https://realpython.com/videos/replace-string-python-method/#t=34.33
00:37https://realpython.com/videos/replace-string-python-method/#t=37.62
If you don’t provide this argument,https://realpython.com/videos/replace-string-python-method/#t=37.62
Python will replace all occurrences of the old string with the new string,https://realpython.com/videos/replace-string-python-method/#t=39.54
and that’s what you want to do with your task at hand.https://realpython.com/videos/replace-string-python-method/#t=44.54
00:47https://realpython.com/videos/replace-string-python-method/#t=47.42
So you won’t see the third argument in this course.https://realpython.com/videos/replace-string-python-method/#t=47.42
00:51https://realpython.com/videos/replace-string-python-method/#t=51.22
At the bottom of this slide,https://realpython.com/videos/replace-string-python-method/#t=51.22
you can see an example of the .replace() method with two arguments.https://realpython.com/videos/replace-string-python-method/#t=52.56
There is a string saying "Fake Python",https://realpython.com/videos/replace-string-python-method/#t=56.57
and you’re calling the .replace() method on the str object.https://realpython.com/videos/replace-string-python-method/#t=59.66
01:03https://realpython.com/videos/replace-string-python-method/#t=63.22
The first argument is the string "Fake. That’s the string you want to replace.https://realpython.com/videos/replace-string-python-method/#t=63.22
And the second argument is the string "Real".https://realpython.com/videos/replace-string-python-method/#t=68.26
That’s the string you want to replacehttps://realpython.com/videos/replace-string-python-method/#t=71.06
the word "Fake" with. The output in the shell is 'Real Python'.https://realpython.com/videos/replace-string-python-method/#t=72.74
01:17https://realpython.com/videos/replace-string-python-method/#t=77.42
That’s much better than Fake Python, in my opinion, but obviously I’m biased.https://realpython.com/videos/replace-string-python-method/#t=77.42
Anyway, there is one important detail here.https://realpython.com/videos/replace-string-python-method/#t=82.98
Although the Python shell displacedhttps://realpython.com/videos/replace-string-python-method/#t=86.32
the result of .replace(), the string itself stays unchanged.https://realpython.com/videos/replace-string-python-method/#t=88.0
01:32https://realpython.com/videos/replace-string-python-method/#t=92.7
Let’s explore what this means. First,https://realpython.com/videos/replace-string-python-method/#t=92.7
you define a variable named name and set it with the string "Fake Python".https://realpython.com/videos/replace-string-python-method/#t=97.13
01:43https://realpython.com/videos/replace-string-python-method/#t=103.1
Now you can use .replace() on name and add the two strings,https://realpython.com/videos/replace-string-python-method/#t=103.1
"Fake" and "Real", as arguments. When you hit Enter,https://realpython.com/videos/replace-string-python-method/#t=106.87
you can see the output 'Real Python'. If you had to guess,https://realpython.com/videos/replace-string-python-method/#t=111.02
what do you think the value of name is now? "Real Python"? Well,https://realpython.com/videos/replace-string-python-method/#t=115.97
actually not. The variable name still has the value of "Fake Python".https://realpython.com/videos/replace-string-python-method/#t=121.35
02:06https://realpython.com/videos/replace-string-python-method/#t=126.47
When you use the .replace() method on a string,https://realpython.com/videos/replace-string-python-method/#t=126.47
the original value of the variable doesn’t change.https://realpython.com/videos/replace-string-python-method/#t=129.1
So what can you do instead?https://realpython.com/videos/replace-string-python-method/#t=132.9
If you assign the result of name.replace() back to the name variable,https://realpython.com/videos/replace-string-python-method/#t=135.43
the value is updated accordingly. Let’s check name again.https://realpython.com/videos/replace-string-python-method/#t=140.19
02:25https://realpython.com/videos/replace-string-python-method/#t=145.93
Okay, now name has the value "Real Python". Cool.https://realpython.com/videos/replace-string-python-method/#t=145.93
By assigning the result of name.replace() back to the name variable,https://realpython.com/videos/replace-string-python-method/#t=150.22
the value of name was updated to "Real Python".https://realpython.com/videos/replace-string-python-method/#t=155.26
02:39https://realpython.com/videos/replace-string-python-method/#t=159.15
This distinction is crucial to keep in mind when working with the .replace()https://realpython.com/videos/replace-string-python-method/#t=159.15
method. When you use .replace() on a string,https://realpython.com/videos/replace-string-python-method/#t=162.64
the original value of the variable doesn’t change.https://realpython.com/videos/replace-string-python-method/#t=167.85
02:51https://realpython.com/videos/replace-string-python-method/#t=171.25
So remember that you always need to assign the result of .replace() to a variablehttps://realpython.com/videos/replace-string-python-method/#t=171.25
if you want to continue to work with the output. Okay,https://realpython.com/videos/replace-string-python-method/#t=176.33
now that we’ve got this covered, it’s time to apply this knowledge to the chat transcript.https://realpython.com/videos/replace-string-python-method/#t=180.6
Become a Memberhttps://realpython.com/account/join/
https://realpython.com/videos/replace-string-python-transcript/
Overviewhttps://realpython.com/courses/replace-string-python/
https://realpython.com/lessons/replace-string-python-chain/
Replace a String in Python (Overview) 01:48 https://realpython.com/videos/replace-string-python-overview/
Investigate the Transcript 02:02 https://realpython.com/videos/replace-string-python-transcript/
Replace a String With .replace() 03:06 https://realpython.com/videos/replace-string-python-method/
Chain the .replace() Method 02:33 https://realpython.com/lessons/replace-string-python-chain/
Set Up Multiple Replacement Rules 04:14 https://realpython.com/lessons/replace-string-python-multiple-rules/
Use a Regex Visualizer 01:59 https://realpython.com/lessons/replace-string-python-regex-visualizer/
Find John Doe 02:14 https://realpython.com/lessons/replace-string-python-john-doe/
Work With a Token 02:04 https://realpython.com/lessons/replace-string-python-token/
Be Case-Insensitive 02:08 https://realpython.com/lessons/replace-string-python-case-insensitive/
Look for a Character Set 01:55 https://realpython.com/lessons/replace-string-python-character-set/
Append a Quantifier 02:18 https://realpython.com/lessons/replace-string-python-quantifier/
Recap Your Regex Patterns 00:13 https://realpython.com/lessons/replace-string-python-regex-patterns/
Get to Know re.sub() 01:09 https://realpython.com/lessons/replace-string-python-resub-intro/
Use re.sub() 05:11 https://realpython.com/lessons/replace-string-python-resub/
Replace a String in Python (Summary) 02:08 https://realpython.com/lessons/replace-string-python-summary/
Privacy Policyhttps://realpython.com/privacy-policy/

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.