How To Escape ‘{}’ Curly braces In A String?

How To Escape '{}' Curly Braces In A String

The curly braces are used everywhere in Python. Most of the time, you can see these curly braces {} during dictionary, or set initialization. The basic syntax of the dictionary that contains key-value pairs is enclosed within this curly braces {}. But, in strings, we can use these curly braces to replace the value in the strings. In this article, we will see how to escape the curly braces {} in a string.

There are various methods in Python to escape this curly-braces ‘{}’ like format method, and f-string method. Let’s see these methods and implementations.

Use of Curly Braces in Python

Dictionary initialization

The curly braces are used to initialize the dictionaries in Python. All the key-value pairs are enclosed within the curly braces. The basic syntax of the dictionary involves this curly braces {} in the code.

Set Initialization

The basic syntax of a set also involves the use of curly braces. This set is a type of collection of elements arranged in an unordered manner.

Formatting of Strings

In this article, we will see the use of curly braces for string formatting. The curly braces are used to replace the value using the format method and f-string methods. This is a very useful method for the customization of strings. Let’s see how these curly braces will escape from the strings.

Format Method to Escape Curly Braces {} in a String

Format method is a built-in method in Python that helps to replace the values from the string. Usually, this format method is used for the customization of the strings. For example, using this format method, we can add different string values, float numbers, characters, or integers to the string after providing curly braces. Here, the curly braces are used instead of the values in the string. During execution, these values will be replaced with the help of the format method.

Name = "Snehal"
Age = 7
Result = "Roll No.is {1} and my name is {0}".format(Name, Age)
print(Result)

This code replaces the curly braces with the variables provided in the list of arguments. These arguments are name and age. .format method here is used to replace the value. Let’s see the implementation.

Format Method To Escape Curly Braces
Format Method To Escape Curly Braces

This format method successfully replaces the curly braces from the string.

F-string Method to Escape Curly Braces {} in a String

The f-string method is very similar to the format method. The f-string is a formatted string literal. This method uses ‘f’ as a prefix before a string. The values initialized at the start of the code will be replaced using the f-string method. This f-string method can replace the curly braces using integers, characters, floats, or string values.

Name = "Snehal"
Roll_No = 7
Result = f"My name is {Name} and my roll No is {Roll_No}."
print(Result)

In this code, the ‘f’ is used as a prefix in the string. The f-string method replaces the Name and Roll_No values from the string. Let’s see the results.

F String To Escape Curly Braces In A String
F String To Escape Curly Braces In A String

In the results, the curly braces are successfully replaced by the values in the output.

Double Curly Braces in Python

The double curly braces in Python print only one pair of curly braces in the string. For example, this double curly braces technique is interpreted as an escape character in Python. Simply put, one pair of curly braces will be eliminated from the input. Let’s see the execution to understand the technique.

Value = "{{ Roll No: }}{0} ".format(7)
print(Value)

This code will replace the double curly braces with single curly braces. Here, the format method includes a number in the string. Let’s see the results.

Double Curly Braces
Double Curly Braces

In this output, only one pair of curly brace is printed.

Summary

This article covers the methods to escape curly braces in Python. This article contains three different methods: format method, f-string method, or double curly braces technique, to escape the curly braces from the string. The use of curly braces in Python is also covered in this article. Hope you will enjoy this article.

References

Do read the official documentation of formatting strings for more details.