Jump to content

Python String translate() Method

Recommended Posts

Strings” are a common data type in Python, and they can be used for text handling, data analysis, etc. Python provides several methods for string manipulation. One such method for string manipulation is the “translate()” method. This method replaces or removes certain characters in a string according to a mapping table.

This write-up presents an in-depth guide on Python “string.translate()” method using the below content:

What is the “string.translate()” Method in Python?

Python’s “string.translate()” method takes the translation table and retrieves a string whose character values are mapped to their associated characters. The “maketrans()” method, however, is utilized to create/make a translation table.





In the above syntax, the “table” parameter can be a dictionary or a mapping table, specifying how the replacement will be done.

Return Value

The “string.translate()” retrieves a string where each character/letter is mapped to its corresponding character according to the translation table.

Example 1: Replacing a Specific Value Using the “string.translate()” Method

The below code is used to replace a specific value by utilizing the mapping table:

string_a = "Python Guide"
print(string_a.translate(str.maketrans("P", "T")))


In the above code:

  • The “maketrans()” method is used to create a mapping table that describes the replacement process i.e., replacement character.
  • The “translate()” method takes the mapping table and returns the string by mapping each character to the specified character.



As seen, the target character has been replaced successfully with the specified one.

Example 2: Replacing Multiple Values Using the “string.translate()” Method

The following code is used to replace multiple values from the Python string using the discussed method:

string_a = "Python Guide"
map_table = string_a.maketrans({'t':'2','o':'7','i':'3','h':'4'})


According to the above code:

  • The “maketrans()” method accepts the dictionary and creates a mapping table.
  • The “translate()” method accepts the mapping table and retrieves the string by replacing all the specified characters with the corresponding character specified in the dictionary.



The multiple characters in the given string have been replaced with their corresponding allocated values.

Example 3: Mapping Value to None Using the “string.translate()” Method

Let’s overview the following code:

string_a = "%Python% #$Guide$#"
map_table = string_a.maketrans('P', 'C', '%#$')


In the above example code:

  • The “maketrans()” method takes three arguments and creates a mapping table.
  • The first argument character is replaced with the second argument character, while the third argument value is mapped to none.
  • The “translate()” method takes the mapping table as an argument and translates the string based on the translation table.



The specified character has been replaced and mapped to none.


The “string.translate()” method in Python takes the translation table and returns a string mapped to the character values. The translation table is created via the “string.maketrans()” method. The “string.translate()” method maps both the single or multiple-character values with the specified character by accepting the translation table or dictionary. This write-up presented a thorough guide on Python’s “string.translate()” method using numerous examples.

View the full article

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...