alternative
  • Home (current)
  • About
  • Tutorial
    Technologies
    C#
    Deep Learning
    Statistics for AIML
    Natural Language Processing
    Machine Learning
    SQL -Structured Query Language
    Python
    Ethical Hacking
    Placement Preparation
    Quantitative Aptitude
    View All Tutorial
  • Quiz
    C#
    SQL -Structured Query Language
    Quantitative Aptitude
    Java
    View All Quiz Course
  • Q & A
    C#
    Quantitative Aptitude
    Java
    View All Q & A course
  • Programs
  • Articles
    Identity And Access Management
    Artificial Intelligence & Machine Learning Project
    How to publish your local website on github pages with a custom domain name?
    How to download and install Xampp on Window Operating System ?
    How To Download And Install MySql Workbench
    How to install Pycharm ?
    How to install Python ?
    How to download and install Visual Studio IDE taking an example of C# (C Sharp)
    View All Post
  • Tools
    Program Compiler
    Sql Compiler
    Replace Multiple Text
    Meta Data From Multiple Url
  • Contact
  • User
    Login
    Register

Python - String Method - maketrans() Tutorial

This method will return a mapping table that can be used with the translate() method to replace the specified characters.

Syntax-

String.maketrans(x, y, z)

Note - It is case sensitive
#x - If only one argument is passed, then it has to be a dictionary.
     If more than one argument, then it has a string specifying the 
     characters you want to replace.
#y - (Optional) It has to be a string of the same length as argument x. 
      Each character in the first argument will be replaced with
      the corresponding character is given in argument y.
#z - (Optional) It describes which character need to remove from original string

Example-

str1 = "Hello Jack"

x = "cKa"
y = "lLI"

mytable = str1.maketrans(x, y)
print(mytable)
print(str1.translate(mytable))

 Output-

{99: 108, 75: 76, 97: 73}
Hello JIlk

In the above example-

"c" is replace with "l"

"K" isn't available in original string because of case sensitive, therefore no changes has been done.

And a" is replaced with "I" with the help of  maketrans() method.

maketrans() method will return a mapping table( i.e unicode of each character) in the form of dictionary. To convert it into string translate() method has to be used.

First two argument should have equal length

str1 = "Hello Jack"

x = "cKa"
y = "lLIOyt"

mytable = str1.maketrans(x, y)
print(str1.translate(mytable))

Output-

ValueError: the first two maketrans arguments must have equal length

In the above example-

both the argument x and y are of different length.  Hence, it will raise a ValueError exception.

maketrans() method with third argument

str1 = "Hello Jack"

x = "cKa"
y = "lLe"
z = "oe"

mytable = str1.maketrans(x, y, z)
print(str1.translate(mytable))
print(str1)

Output-

Hll Jelk
Hello Jack

In the above example-

First, third argument z will remove character "o" and "e" ,then the character of argument x will be replaced by argument y.

 

 

Python

Python

  • Introduction
  • Installation and running a first python program
    • How to install Python ?
    • Running 1st Hello World Python Program
    • How to install Pycharm ?
  • Things to know before proceed
    • Escape Characters/Special Characters
    • Syntax ( Indentation, Comments )
    • Variable
    • Datatype
    • Keyword
    • Literals
    • Operator
    • Precedence & Associativity Of Operator
    • Identifiers
    • Ascii, Binary, Octal, Hexadecimal
    • TypeCasting
    • Input, Output Function and Formatting
  • Decision control and looping statement
    • if-else
    • for loop
    • While loop
    • Break Statement
    • Continue Statement
    • Pass Statement
  • Datatype
    • Number
    • List
    • Tuple
    • Dictionary
    • String
    • Set
    • None & Boolean
  • String Method
    • capitalize()
    • upper()
    • lower()
    • swapcase()
    • casefold()
    • count()
    • center()
    • endswith()
    • split()
    • rsplit()
    • title()
    • strip()
    • lstrip()
    • rstrip()
    • find()
    • index()
    • format()
    • encode()
    • expandtabs()
    • format_map()
    • join()
    • ljust()
    • rjust()
    • maketrans()
    • partition()
    • rpartition()
    • translate()
    • splitlines()
    • zfill()
    • isalpha()
    • isalnum()
    • isdecimal()
    • isdigit()
    • isidentifier()
    • islower()
    • isupper()
    • isnumeric()
    • isprintable()
    • isspace()
    • istitle()
  • Python Function
    • Introduction
  • Lambda Function
    • Lambda Function
  • Python Advanced
    • Iterators
    • Module
    • File Handling
    • Exceptional Handling
    • RegEx - Regular Expression
    • Numpy
    • Pandas

About Fresherbell

Best learning portal that provides you great learning experience of various technologies with modern compilation tools and technique

Important Links

Don't hesitate to give us a call or send us a contact form message

Terms & Conditions
Privacy Policy
Contact Us

Social Media

© Untitled. All rights reserved. Demo Images: Unsplash. Design: HTML5 UP.

Toggle