|
| https://realpython.com/ |
| Start Here | https://realpython.com/start-here/ |
|
Learn Python
| https://realpython.com/videos/replace-string-python-method/ |
| Python Tutorials →In-depth articles and video courses | https://realpython.com/search?kind=article&kind=course&order=newest |
| Learning Paths →Guided study plans for accelerated learning | https://realpython.com/learning-paths/ |
| Quizzes & Exercises →Check your learning progress | https://realpython.com/quizzes/ |
| Browse Topics →Focus on a specific area or skill level | https://realpython.com/tutorials/all/ |
| Community Chat →Learn with other Pythonistas | https://realpython.com/community/ |
| Office Hours →Live Q&A calls with Python experts | https://realpython.com/office-hours/ |
| Podcast →Hear what’s new in the world of Python | https://realpython.com/podcasts/rpp/ |
| Books →Round out your knowledge and learn offline | https://realpython.com/products/books/ |
| Reference →Concise definitions for common Python terms | https://realpython.com/ref/ |
| Code Mentor →BetaPersonalized code assistance & learning tools | https://realpython.com/mentor/ |
| Unlock All Content → | https://realpython.com/account/join/ |
|
More
| https://realpython.com/videos/replace-string-python-method/ |
| Learner Stories | https://realpython.com/learner-stories/ |
| Python Newsletter | https://realpython.com/newsletter/ |
| Python Job Board | https://www.pythonjobshq.com |
| Meet the Team | https://realpython.com/team/ |
| Become a Contributor | https://realpython.com/jobs/ |
| Search | https://realpython.com/search |
| https://realpython.com/search |
| Join | https://realpython.com/account/join/ |
| Sign‑In | https://realpython.com/account/login/?next=%2Fvideos%2Freplace-string-python-method%2F |
| https://realpython.com/courses/replace-string-python/#team |
| Replacing a String in Python | https://realpython.com/courses/replace-string-python/ |
| Philipp Acsany | https://realpython.com/courses/replace-string-python/#team |
| Recommended Tutorial | https://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 Question | https://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 |
| Transcript | https://realpython.com/videos/replace-string-python-method/#transcript |
| Discussion | https://realpython.com/videos/replace-string-python-method/#discussion |
| 00:00 | https://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 as | https://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:15 | https://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() string | https://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:26 | https://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:37 | https://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:47 | https://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:51 | https://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:03 | https://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 replace | https://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:17 | https://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 displaced | https://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:32 | https://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:43 | https://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:06 | https://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:25 | https://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:39 | https://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:51 | https://realpython.com/videos/replace-string-python-method/#t=171.25 |
| So remember that you always need to assign the result of .replace() to a variable | https://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 Member | https://realpython.com/account/join/ |
| https://realpython.com/videos/replace-string-python-transcript/ |
| Overview | https://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 Policy | https://realpython.com/privacy-policy/ |
Viewport: width=device-width, initial-scale=1, shrink-to-fit=no, viewport-fit=cover