---
title: maketrans()
url: https://docs.vultr.com/python/standard-library/str/maketrans
description: Create Translation Table
publish_date: 2024-12-25T20:40:00.085772Z
last_updated: 2024-12-25T20:40:00.085772Z
header_image: https://docs.vultr.com/public/doc-assets/collection-items/1631/a6de6e0d-ab7c-4118-ad85-4d0e930c2b44.jpg
---

> Index of every Vultr Docs page in markdown: https://docs.vultr.com/llms.txt

# Python str maketrans() - Create Translation Table

## Introduction
The `maketrans()` method in Python is a static method of the `str` class that creates a translation table, which is a mapping of integers or characters to integers, strings, or None. This translation table can then be used with Python's `translate()` method to modify strings. The `maketrans()` method is particularly useful for data cleaning, preprocessing in machine learning, or anywhere where string manipulation is necessary.

In this article, you will learn how to create and utilize translation tables using the `maketrans()` and `translate()` methods in Python. Discover how to replace, remove, or modify characters in strings effectively through practical examples and step-by-step guidance.

## Understanding str maketrans()

### Syntax and Parameters
1. Examine the typical syntax of the `maketrans()` method:

    ```python
    str.maketrans(x[, y[, z]])
    ```

1. Understand the parameters:
   * `x` can be a dictionary describing a one-to-one mapping of Unicode ordinals or characters to Unicode ordinals, strings, or `None`.
   * `y` is optional and represents a string of characters to map to the string of equal length `z`.
   * `z` is optional and represents a string where each character in the string is mapped to None in the translation table.

### Types of Input for maketrans()
1. **Using a Dictionary:**
   Define a dictionary where each key-value pair represents the character to be replaced and the character to replace it with.

    ```python
    translate_dict = {'a': '1', 'e': '2'}
    trans_table = str.maketrans(translate_dict)
    ```

1. **Using two Strings:**
   Define two strings of equal length where characters in the first string are replaced by characters in the same position in the second string.

    ```python
    trans_table = str.maketrans('aeiou', '12345')
    ```

1. **Using three Strings (to remove characters):**
   Define a third string where each character in this string will be mapped to None.

    ```python
    trans_table = str.maketrans('aeiou', '12345', 'xyz')
    ```

### Applications of Translation Tables
1. **Character Replacement:** Change specific characters to other characters.
1. **Data Cleaning:** Remove unwanted characters from strings.
1. **Character Mapping:** Standardize character sets in text processing tasks.

## Implementing maketrans() and translate()

### Basic Translation and Replacement
1. Create a basic translation table.

    ```python
    trans_table = str.maketrans('abc', '123')
    ```

1. Use the translation table with `translate()` to modify a string.

    ```python
    sample_string = 'Hello, beautiful world and amazing people!'
    result = sample_string.translate(trans_table)
    print(result)
    ```

    This will replace 'a' with '1', 'b' with '2', and 'c' with '3' in the `sample_string`.

### Removing Characters
1. Create a translation table that maps characters to None.

    ```python
    trans_table = str.maketrans('', '', 'aeiou')
    ```

1. Apply the translation table to remove all vowels from a string.

    ```python
    sample_string = "Live, Learn, and Thrive!"
    result = sample_string.translate(trans_table)
    print(result)
    ```

    This results in "Lv, Lrn, nd Thrve!", having all vowels removed.

## Conclusion
The `str.maketrans()` method in Python offers a powerful way to create translation tables that efficiently modify strings using the `translate()` method. By following the examples and explanations provided, master replacing, removing, or altering characters in strings for various applications like data cleaning or preprocessing. These operations significantly enhance string manipulation capabilities, making your text processing tasks more efficient and streamlined. Harness the potential of `maketrans()` and `translate()` to make your Python scripting more effective and tailored to your needs.