translate()

All String methods

translate() , maketrans() : Search replace and delete of set of chars.
str_update=str_input.maketrans(str_search,str_replace,str_delete)
str_search : Required, Chars to be searched
str_replace: Required, Chars to be replaced ( mapped to searched chars )
str_delete : Optional, Chars to be replaced with None ( deleted in final string )

Length of str_search should be equal to length of str_replace
the method maketrans() returns a dictionary ( str_update ) with chars mapped for search and replace and this can be used for translate().
str_search='abc'  # search chars 
str_replace='xyz' # replace chars 
str_delete='pqr'  # delete chars 

str_input='abcdefghpqr' #input string 

str_update=str_input.maketrans(str_search,str_replace,str_delete)

print(str_update) # dictionary output with chars mapped. 
print("input string : ",str_input)
print("Output string: ",str_input.translate(str_update))
Output
{97: 120, 98: 121, 99: 122, 112: None, 113: None, 114: None}
input string :  abcdefghpqr
Output string:  xyzdefgh
All String methods


Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer