Alternative to .replace() for replacing multiple substrings in a string

93 views Asked by At

Are there any alternatives that are similar to .replace() but that allow you to pass more than one old substring to be replaced?

I have a function with which I pass video titles so that specific characters can be removed (because the API I'm passing the videos too has bugs that don't allow certain characters):

def videoNameExists(vidName):
    vidName = vidName.encode("utf-8")
    bugFixVidName = vidName.replace(":", "")
    search_url ='https://api.brightcove.com/services/library?command=search_videos&video_fields=name&page_number=0&get_item_count=true&token=kwSt2FKpMowoIdoOAvKj&any=%22{}%22'.format(bugFixVidName)

Right now, it's eliminating ":" from any video titles with vidName.replace(":", "") but I also would like to replace "|" when that occurs in the name string sorted in the vidName variable. Is there an alternative to .replace() that would allow me to replace more than one substring at a time?

2

There are 2 answers

0
Avinash Raj On

You may use re.sub

import re
re.sub(r'[:|]', "", vidName)
0
dlask On
>>> s = "a:b|c"
>>> s.translate(None, ":|")
'abc'