Use of the maketrans () method in Python2.x, python2.xmaketrans

Source: Internet
Author: User

Use of the maketrans () method in Python2.x, python2.xmaketrans

The string intab returned by the maketrans () method maps each character to a conversion table with the same position as the character string outtab. The table is then passed to the translate () function.

Note: Both intab and outtab must have the same length.
Syntax

The syntax of the maketrans () method is as follows:

Str. maketrans (intab, outtab]);

Parameters

  • Intab -- this is the actual character string.
  • Outtab -- this is the string of the corresponding ing characters.

Return Value

This method uses the translate () function to convert the table.
Example

The following example shows how to use the maketrans () method. Here, the position where each vowel in a string replaces its vowel:

#! /Usr/bin/pythonfrom string import maketrans # Required to call maketrans function. intab = "aeiou" outtab = "12345" trantab = maketrans (intab, outtab) str = "this is string example .... wow !!! "; Print str. translate (trantab );

When we run the above program, it will produce the following results:

Th3s 3 s str3ng 2x1mpl2... w4w !!!


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.