Python String translate() Method Example

Python’s string translate function is used with string. In this post, you will learn what is the definition of Python’s string translate function, what is syntax and how to use this function with string in Python programs.

The string translate() method returns a string where each character is mapped to its corresponding character in the translation table. translate() method takes the translation table to replace/translate characters in the given string as per the mapping table.

Python String translate() Function with Example

The python string translate() method returns a string where each character is mapped to its corresponding character in the translation table.

One more thing about the python translate() method, it takes the translation table to replace/translate characters in the given string as per the mapping table.

The syntax of the translate() method is:

string.translate(table)

Parameters of Python String translate()

The translate() method takes a single parameter:

  • table – a translation table containing the mapping between two characters; usually created by maketrans()

The return value from Python String translate()

The translate() method returns a string where each character is mapped to its corresponding character as per the translation table.

Example 1: Translation/Mapping using translation table with translate()

# first string
firstString = "abc"
secondString = "def"
thirdString = "ghi"

string = "xyz"
print("Original string:", string)

translation = string.maketrans(firstString, secondString, thirdString)

# translate string
print("Translated string:", string.translate(translation))

Output

Original string: abcdef
Translated string: idef

Example 2: Python string.translate remove characters

Python string translate() function with ord() function replace each character in the string using the given translation table.

Let’s check the example below:

s = 'abc abc abc'

print(s.translate({ord('a'): None}))

Output

bc bc bc

Example 3: Removing Spaces from a String Using translate() and replace()

string = ' P Y T H O N'
print(string.replace(' ', ''))  # PYTHON
print(string.translate({ord(i): None for i in ' '}))  # PYTHON

Output

PYTHON
PYTHON

Example 4: Python remove newline from string using translate

s = 'ab\ncd\nef'
print(s.replace('\n', ''))
print(s.translate({ord('\n'): None}))

Output

abcdef
abcdef

Recommended Python Tutorials

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *