|
| https://realpython.com/ |
| Start Here | https://realpython.com/start-here/ |
|
Learn Python
| https://realpython.com/lessons/replace-string-python-multiple-rules/ |
| 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/lessons/replace-string-python-multiple-rules/ |
| 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=%2Flessons%2Freplace-string-python-multiple-rules%2F |
| Unlock This Lesson | https://realpython.com/account/join/?utm_source=rp_lesson&utm_content=replace-string-python |
| Unlock This Lesson | https://realpython.com/account/join/?utm_source=rp_lesson&utm_content=replace-string-python |
| 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/lessons/replace-string-python-multiple-rules/#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/lessons/replace-string-python-multiple-rules/#transcript |
| Discussion | https://realpython.com/lessons/replace-string-python-multiple-rules/#discussion |
| 00:00 | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=0.53 |
| There are a few more replacements that you need to make to the transcript to get | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=0.53 |
| it into a format acceptable for independent review. | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=4.32 |
| Shorten or remove the timestamps and replace the usernames with Agent and | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=7.75 |
| Client. Now that you are starting to have more strings to replace, | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=12.04 |
| chaining on .replace() is going to get repetitive. | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=15.92 |
| 00:18 | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=18.86 |
| So let’s explore a better way to handle the replacements. | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=18.86 |
| 00:24 | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=24.14 |
| Start with a file named | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=24.14 |
| transcript_multiple_replace.py and add the transcript as a multiline string. | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=27.01 |
| Okay, now let’s think a moment. | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=32.26 |
| The way .replace() works is that you provide an argument pair. | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=34.57 |
| 00:38 | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=38.37 |
| The first argument is the string that you want to replace, | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=38.37 |
| and the second argument is the replacement string. | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=41.61 |
| That sounds a lot like a tuple, doesn’t it? | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=45.14 |
| 00:48 | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=48.13 |
| So one idea could be to keep a list of tuples with two items in each tuple. | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=48.13 |
| The two items would correspond to the arguments that you need to pass into the | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=53.77 |
| .replace() method, the string to replace, and the replacement string. | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=57.71 |
| 01:02 | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=62.41 |
| So let’s try that out. | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=62.41 |
| 01:04 | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=64.97 |
| We make a list named replacements and add in the tuples. | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=64.97 |
| The first one contains "BLASTED" and the huffing emoji. | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=69.17 |
| The second one contains "Blast" and the huffing emoji. | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=72.69 |
| 01:16 | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=76.33 |
| The first "BLASTED" is in uppercase, | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=76.33 |
| and the "Blast" right now is with an uppercase letter at the start. | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=78.37 |
| Then you add the first part of the timestamp, | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=82.26 |
| so that’s "2022-08-24" | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=85.21 |
| and then uppercase "T", and an empty string ("") to replace it. | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=89.53 |
| 01:35 | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=95.26 |
| Then you need to add the "+00:00". | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=95.26 |
| You also replace this with an empty string. The "[support_tom]" username, | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=99.45 |
| which you replace with "Agent ". | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=104.52 |
| 01:46 | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=106.76 |
| Don’t forget the space behind the Agent because that will align the columns | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=106.76 |
| better. And the tuple "[johndoe]" that you replace with "Client". | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=110.35 |
| 01:55 | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=115.78 |
| Note that Client doesn’t have a space at the end, so this way, | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=115.78 |
| "Client" and "Agent " with a space have both the same amount of characters and will | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=119.41 |
| align nicely in your clean transcript. | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=123.95 |
| 02:07 | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=127.96 |
| "[support_tom]" and "[johndoe"] both need to be in square brackets. | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=127.96 |
| Then you have a square bracket in line 17 to close the replacements list. | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=131.25 |
| 02:17 | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=137.13 |
| With the list of replacements in place, | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=137.13 |
| you can iterate over the list and call .replace() on the transcript string. | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=139.68 |
| So, for old, new in replacements: | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=144.2 |
| 02:29 | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=149.06 |
| transcript = transcript.replace(old, new) | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=149.06 |
| and then you print(transcript) in the end. | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=155.39 |
| 02:39 | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=159.03 |
| In this version of your transcript-cleaning script, | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=159.03 |
| you created a list of replacement tables, | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=161.55 |
| which gives you a quick way to add replacements. | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=163.69 |
| You then iterate over the list of replacement tuples. | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=166.71 |
| 02:49 | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=169.71 |
| In each iteration, you call .replace() on the string, populating the | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=169.71 |
| arguments with the old and new variables that have been unpacked from each | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=174.65 |
| replacement table. With this, | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=178.69 |
| you’ve made a big improvement in the overall readability of the transcript. | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=180.97 |
| 03:05 | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=185.59 |
| It’s also easier to add replacements if you need to. | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=185.59 |
| So let’s run the script and see what happens. | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=189.14 |
| 03:13 | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=193.68 |
| Once you’re in the terminal, | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=193.68 |
| you can run python and then the name of your Python file, | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=194.59 |
| which is transcript_multiple_replace.py. | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=197.26 |
| 03:21 | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=201.98 |
| And that’s a pretty clean transcript. | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=201.98 |
| You replace the swear words with the huffing emoji, and you replace the usernames | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=204.94 |
| with Agent and Client. Also, the timestamps are much more readable now. | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=209.52 |
| 03:34 | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=214.55 |
| Well done. | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=214.55 |
| Your script works perfectly with the provided transcript that you’ve got, | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=216.11 |
| but you may ask, what if you get a different transcript later that day? | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=220.62 |
| 03:45 | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=225.57 |
| Maybe there is another agent or another client. Also, | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=225.57 |
| replacing the swear words won’t work if there’s another variation—for example, | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=229.99 |
| using "ing" or with a different capitalization. And you are right. | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=233.76 |
| 03:58 | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=238.49 |
| These are valid concerns. But you know what? | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=238.49 |
| I got you covered, just as if I already knew what concerns you would have. | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=241.91 |
| What a coincidence. | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=246.98 |
| You’ll learn how to handle all of these concerns in the next lesson. | https://realpython.com/lessons/replace-string-python-multiple-rules/#t=248.73 |
| Become a Member | https://realpython.com/account/join/ |
| https://realpython.com/lessons/replace-string-python-chain/ |
| Overview | https://realpython.com/courses/replace-string-python/ |
| https://realpython.com/lessons/replace-string-python-regex-visualizer/ |
|
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