Skip to content

[Python] Use format_map() To Fill Variable Values In The String

Recently, I trace many codes about LLaMA LoRA training (almost two months ago…), and when I reading the codes, I found if they want to configure the prompt for LLMs, if they need to change the prompt content by different training, the format_map() function is the best choice.

So, today I want to record something about format_map() function!


Sample Code

Simply put, format_map() can fill the original string if they are keep the null variables, and become to the new string.

For example, if we set two variables like {Name} and {Age}, and we have a lot of training data, we can use for-loop to get the new combined string.

# coding: utf-8


def main() -> None:
    data = [
        {"Name": "Clay", "Age": 29},
        {"Name": "Atlas", "Age": 27},
        {"Name": "Wendy", "Age": 20},
    ]

    original_text = "My name is {Name} and I am {Age} years old."

    for item in data:
        print(original_text.format_map(item))


if __name__ == "__main__":
    main()


Output:

My name is Clay and I am 29 years old.
My name is Atlas and I am 27 years old.
My name is Wendy and I am 20 years old.


If you do not know the syntax, we also can do it like:

for item in data:
    print("My name is {} and I am {} years old.".format(item["Name"], item["Age"]))
for item in data:
    print(f"My name is {item['Name']} and I am {item['Age']} years old.")
for item in data:
    print(f"My name is " + item["Name"] + " and I am " + str(item["Age"]) + " years old.")

The first method can look very messy when there are many variables.

The second method is fairly good and has good readability; it’s just that when it comes to conditionally switching different variables for filling, it’s not as convenient as format_map(), and might require modifying the original data.

The third method has the worst readability.

Above are some Python tips for concatenating strings.


References


Read More

Tags:

Leave a Reply