Add or Remove Brackets around a String in Python

  • Post category:String

In this article, you will see how to add or remove brackets around a string in Python. The brackets are used to mark the beginning and end of a list. If you want to add brackets to a string, you can use the string format(). The brackets can be removed from a string using the strip() method.

Step 1: Create a string

str = "Python"
print("str = ", str)
print("str type = ", type(str))

Step 2: Add brackets around a string

print("str = ", "[{}]".format(str))

Step 3: Remove brackets around a string

print("str = ", str.strip("[]"))

Output:

str =  Python
str type =  <class 'str'>
str =  [Python]
str =  Python

Free resources to learn advanced skills: AiHints and CodeAllow