Python String maketrans() Method Last Updated : 08 Jan, 2025 Comments Improve Suggest changes Like Article Like Report maketrans() method in Python is a powerful tool for creating mapping tables that specify how specific characters in a string should be replaced. This method is often used in conjunction with the translate() method to perform character replacements efficiently. Let's understand with help of an example: Python # Creating a translation table t = str.maketrans("abc", "xyz") # Translating a string res = "abcde".translate(t) print(res) Outputxyzde Explanation:maketrans() method creates a mapping where a is replaced with x, b with y, and c with z.translate() method applies this mapping to the string"abcde", resulting in the transformed string "xyzde".Syntax of maketrans() Methodstr.maketrans(x[, y[, z]])Parametersx: A string, dictionary, or sequence of characters that need to be replaced.y (optional): A string with the same length as x, specifying the replacement characters.z (optional): A string containing characters to be removed.Return TypeReturns a dictionary-like mapping table that can be passed to the translate() method.Examples of maketrans() Method1. Basic character mappingCharacter mapping refers to associating one set of characters or symbols with another. Replacing vowels with corresponding uppercase letters. Python # Create a translation table t = str.maketrans("aeiou", "AEIOU") # Translating a string res = "Learn Python with GFG".translate(t) print(res) OutputLEArn PythOn wIth GFG Explanationmaketrans() maps lowercase vowels to their uppercase counterparts.translate() applies this mapping, transforming vowels in the string to uppercase.2. Removing unwanted charactersRemoving unwanted characters can be done by using regular expression or simple string operations in Python. Python # Create a translation table with characters to remove t = str.maketrans("", "", ",.!?") # Translating a string res = "Learn Python, with GFG!".translate(t) print(res) OutputLearn Python with GFG Explanationmaketrans() specifies characters to be removed using the third parameter.translate() eliminate these characters from the string.3. Using a dictionary for mappingThis is useful if we need to replace specific words represented by their initial letters. Python # Create a translation table using a dictionary t = str.maketrans({"L": "D", "w": "v"}) # Translating a string res = "Learn Python with GFG".translate(t) print(res) OutputDearn Python vith GFG ExplanationThe dictionary specifies the replacements.The mapping is applied directly to the string, replacing specified characters. Comment More infoAdvertise with us Next Article Python String maketrans() Method A AmiyaRanjanRout Follow Improve Article Tags : Python Python-Built-in-functions python-string Python-string-functions Practice Tags : python Similar Reads Python String Methods Python string methods is a collection of in-built Python functions that operates on strings.Note: Every string method in Python does not change the original string instead returns a new string with the changed attributes. Python string is a sequence of Unicode characters that is enclosed in quotatio 5 min read Python Strings decode() method The decode() method in Python is used to convert encoded text back into its original string format. It works as the opposite of encode() method, which converts a string into a specific encoding format. For example, let's try to encode and decode a simple text message:Pythons = "Geeks for Geeks" e = 4 min read turtle.backward() method in Python The turtle module provides turtle graphics primitives, in both object-oriented and procedure-oriented ways. Because it uses Tkinter for the underlying graphics, it needs a version of Python installed with Tk support. turtle.backward() The turtle.backward() method is used to move the turtle backward 1 min read shutil.copytree() method - Python shutil.copytree() method in Python is used to recursively copy an entire directory tree from a source to a destination. It copies all the contents, including files and subdirectories, preserving the directory structure.Syntaxshutil.copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2, 3 min read Python String translate() method Python String translate() method returns a modified string replacing the characters described in a dictionary, or in a hash table. Syntax: string.translate(hash map) Parameters string- Original String hash map- mapping between two characters in the original string.Python String translate() method Ex 3 min read Python | Decimal shift() method Decimal#shift() : shift() is a Decimal class method which returns the shifted copy of x, y times Syntax: Decimal.shift() Parameter: Decimal values Return: the shifted copy of x, y times Code #1 : Example for shift() method Python3 # Python Program explaining # shift() method # loading decimal librar 2 min read turtle.left() method in Python The turtle module provides turtle graphics primitives, in both object-oriented and procedure-oriented ways. Because it uses Tkinter for the underlying graphics, it needs a version of Python installed with Tk support. turtle.left() The turtle.left() method is used to change the direction of the turtl 2 min read Python bytes() method bytes() method in Python is used to create a sequence of bytes. In this article, we will check How bytes() methods works in Python. Pythona = "geeks" # UTF-8 encoding is used b = bytes(a, 'utf-8') print(b)Outputb'geeks' Table of Contentbytes() Method SyntaxUsing Custom EncodingConvert String to Byte 3 min read turtle.up() method in Python The turtle module provides turtle graphics primitives, in both object-oriented and procedure-oriented ways. Because it uses Tkinter for the underlying graphics, it needs a version of Python installed with Tk support. turtle.up() The turtle.up() method is used to pull the pen up from the screen. It g 1 min read turtle.pos() method in Python The Turtle module provides turtle graphics primitives, in both object-oriented and procedure-oriented ways. Because it uses Tkinter for the underlying graphics, it needs a version of Python installed with Tk support. turtle.pos() This method is used to find the turtle's current location (x, y), as a 2 min read Like