{"id":3722,"date":"2022-12-02T18:52:18","date_gmt":"2022-12-02T18:52:18","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=3722"},"modified":"2022-12-02T18:52:19","modified_gmt":"2022-12-02T18:52:19","slug":"python-string-maketrans","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/python-string-maketrans\/","title":{"rendered":"Python String maketrans() Method With Example"},"content":{"rendered":"<p style=\"text-align: center;\">Python String maketrans<\/p>\n<p>Now, we are going to learn about the maketrans method. The maketrans method returns a mapping table, usable for the translate method. If we are specifying only one argument here, it should be a dictionary mapping Unicode ordinals(integers) or characters to the Unicode ordinals, strings, or None. Note that this method is a static method.<\/p>\n<p>If we have two arguments, the arguments should be strings of equal length, and in the resulting dictionary, each character in one string would be mapped with the character at the same position in another string. If we have a third argument, it should be a string, whose characters will be mapped to None in the result.<\/p>\n<p>Let\u2019s have a look at a simple example, which would make us understand the maketrans method.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3723 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-maketrans-METHOD.jpg\" alt=\"\" width=\"629\" height=\"143\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-maketrans-METHOD.jpg 629w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-maketrans-METHOD-300x68.jpg 300w\" sizes=\"auto, (max-width: 629px) 100vw, 629px\" \/><\/p>\n<h2 style=\"text-align: center;\">Python String maketrans() Method<\/h2>\n<p>As you can see in the above program, we have a dictionary, and then we are calling the maketrans method on some string, and we are passing the dictionary to the maketrans method. Remember that if we are passing one argument, it must be a dictionary.<\/p>\n<p>As an output, we get a mapping table from the maketrans method. Let\u2019s have a look at the output now.<\/p>\n<p>{65: &#8216;G&#8217;, 66: &#8216;H&#8217;, 67: &#8216;I&#8217;}<\/p>\n<p>As you can see that we have got the mapping table. Remember that here, we are passing only one argument to the maketrans method, and in such a situation, we must pass a dictionary here.<\/p>\n<p>Now, Let\u2019s say that we are passing two arguments. In such a case, we should give two strings of the same length. Let\u2019s have a look at a simple program, which demonstrates the same thing.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3724 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-maketrans-METHOD-1.jpg\" alt=\"\" width=\"621\" height=\"93\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-maketrans-METHOD-1.jpg 621w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-maketrans-METHOD-1-300x45.jpg 300w\" sizes=\"auto, (max-width: 621px) 100vw, 621px\" \/><\/p>\n<p>As you can see in the above program, we have two strings, with the same lengths, and then we are passing those as arguments to the maketrans method. If you try to give strings of unequal length to the maketrans method, it would cause an error. Let\u2019s have a look at the output of the above program.<\/p>\n<p>{65: 71, 66: 89, 67: 97, 68: 110, 69: 105, 70: 80, 71: 97, 72: 110, 73: 100, 74: 105, 75: 116}<\/p>\n<p>As you can see in the output, we got a mapping table, in which each character from one string would be mapped with the character in the same position in another string.<\/p>\n<p>Also, we can pass a third argument, which also must be a string, and all the characters from that string would be mapped to None. Let\u2019s have a look at the sample program for the same.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3725 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-maketrans-METHOD-2.jpg\" alt=\"\" width=\"629\" height=\"116\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-maketrans-METHOD-2.jpg 629w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-maketrans-METHOD-2-300x55.jpg 300w\" sizes=\"auto, (max-width: 629px) 100vw, 629px\" \/><\/p>\n<p>As you can see in the above program, we are providing three arguments to the maketrans method, and all the arguments are strings. The characters from the third string would be mapped with None. Let\u2019s have a look at the output now.<\/p>\n<p>{65: None, 66: None, 67: None, 68: 110, 69: 105, 70: 80, 71: 97, 72: 110, 73: 100, 74: 105, 75: 116}<\/p>\n<p>As you can see in the output, the characters from the third string are mapped to None. So, as and when required, we can make use of the maketrans method. Basically, it returns a mapping table, which can be usable by the translate method.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python String maketrans Now, we are going to learn about the maketrans method. The maketrans method returns a mapping table, usable for the translate method. If we are specifying only one argument here, it should be a dictionary mapping Unicode ordinals(integers) or characters to the Unicode ordinals, strings, or None. Note that this method is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[26,27],"class_list":{"0":"post-3722","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-python-tutorial","7":"tag-python","8":"tag-python-tutorial"},"_links":{"self":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3722","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/comments?post=3722"}],"version-history":[{"count":2,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3722\/revisions"}],"predecessor-version":[{"id":4200,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3722\/revisions\/4200"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=3722"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=3722"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=3722"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}