Last updated: Apr 9, 2024
Reading timeยท6 min
Use the str.join()
method to remove the square brackets from a list.
The str.join()
method will join the list into a string without the square
brackets.
# โ remove square brackets from a list of Strings list_of_strings = ['bobby', 'hadz', 'com'] result = ', '.join(list_of_strings) print(result) # ๐๏ธ bobby, hadz, com # --------------------------------------------- # โ remove square brackets from a list of Integers list_of_numbers = [44, 22, 66] result = ', '.join(str(item) for item in list_of_numbers) print(result) # ๐๏ธ 44, 22, 66
We used the str.join()
method to remove the square brackets from a list.
If you need to remove the square brackets from a string, click on the following subheading:
The str.join() method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.
Note that the method raises a TypeError
if there are any non-string values in
the iterable.
If your list contains numbers or other types, convert all of the values to
strings before calling join()
.
list_of_numbers = [44, 22, 66] result = ', '.join(str(item) for item in list_of_numbers) print(result) # ๐๏ธ 44, 22, 66
We used a generator expression to iterate over the list.
On each iteration, we use the str() class to convert the number to a string.
The string the join()
method is called on is used as the separator between the
elements.
list_of_numbers = [44, 22, 66] result = ' '.join(str(item) for item in list_of_numbers) print(result) # ๐๏ธ 44 22 66
If you don't need a separator and just want to join the list's elements into a
string, call the join()
method on an empty string.
list_of_numbers = [44, 22, 66] result = ''.join(str(item) for item in list_of_numbers) print(result) # ๐๏ธ 442266
You can also use the map()
function to convert all items in the list to
strings before calling join()
.
list_of_integers = [44, 22, 66] result = ''.join(map(str, list_of_integers)) print(result) # ๐๏ธ 442266
The map() function takes a function and an iterable as arguments and calls the function with each item of the iterable.
If you need to remove the square brackets to flatten the list, use a list comprehension.
my_list = [['bobby'], ['hadz'], ['.', 'com']] flat_list = [x for xs in my_list for x in xs] print(flat_list) # ๐๏ธ ['bobby', 'hadz', '.', 'com']
Alternatively, you can use string slicing.
This is a three-step process:
str()
class to convert the list to a string.list_of_strings = ['bobby', 'hadz', 'com'] result = str(list_of_strings)[1:-1] print(result) # ๐๏ธ 'bobby', 'hadz', 'com' # --------------------------------------------- list_of_integers = [44, 22, 66] result = str(list_of_integers)[1:-1] print(result) # ๐๏ธ 44, 22, 66
We used the str()
class to convert the list to a string and used string
slicing to exclude the square brackets.
list_of_strings = ['bobby', 'hadz', 'com'] # "['bobby', 'hadz', 'com']" print(str(list_of_strings))
The syntax for string slicing is my_str[start:stop:step]
.
start
index is inclusive, whereas the stop
index is exclusive (up to, but not including).Python indexes are zero-based, so the first character in a string has an index
of 0
, and the last character has an index of -1
or len(my_str) - 1
.
We used a start
index of 1
to exclude the left square bracket and used a
stop
index of -1
to exclude the right square bracket.
str.replace()
You can also use the str.replace()
method to achieve the same result.
list_of_strings = ['bobby', 'hadz', 'com'] result = str(list_of_strings).replace('[', '').replace(']', '') print(result) # ๐๏ธ 'bobby', 'hadz', 'com'
If you need to remove the square brackets from each item in the list, use the
str.replace()
method.
my_list = ['[bobby]', '[hadz]', '[com]'] result = [item.replace('[', '').replace(']', '') for item in my_list] print(result) # ๐๏ธ ['bobby', 'hadz', 'com']
The str.replace() method returns a copy of the string with all occurrences of a substring replaced by the provided replacement.
The method takes the following parameters:
Name | Description |
---|---|
old | The substring we want to replace in the string |
new | The replacement for each occurrence of old |
count | Only the first count occurrences are replaced (optional) |
The method doesn't change the original string. Strings are immutable in Python.
Use multiple calls to the str.replace()
method to remove the brackets from a
string.
a_string = '[b]obb{y}' new_string = a_string.replace('[', '').replace( ']', '').replace('{', '').replace('}', '') print(new_string) # ๐๏ธ 'bobby'
The example uses multiple calls to the str.replace()
method to remove the
brackets from the string.
The str.replace() method returns a copy of the string with all occurrences of a substring replaced by the provided replacement.
a_string = '[b]obb{y}' new_string = a_string.replace('[', '').replace( ']', '').replace('{', '').replace('}', '') print(new_string) # ๐๏ธ 'bobby'
The method takes the following parameters:
Name | Description |
---|---|
old | The substring we want to replace in the string |
new | The replacement for each occurrence of old |
count | Only the first count occurrences are replaced (optional) |
The method doesn't change the original string. Strings are immutable in Python.
You can chain as many calls to the str.replace()
method as necessary.
str.strip()
If the brackets are only at the start or end of the string, you can also use the
str.strip()
method.
a_string = '[{bobby}]' new_string = a_string.strip('][}{') print(new_string) # ๐๏ธ bobby
The str.strip() method returns a copy of the string with the specified leading and trailing characters removed.
Alternatively, you can use the re.sub()
method.
The re.sub()
method will remove the brackets from a string by replacing them
with empty strings.
import re a_string = '[b](o)bb{y}' # ๐๏ธ using backslash to escape square brackets new_string = re.sub(r'[()\[\]{}]', '', a_string) print(new_string) # ๐๏ธ bobby
The re.sub method returns a new string that is obtained by replacing the occurrences of the pattern with the provided replacement.
The first argument we passed to the re.sub()
method is a regular expression.
The square brackets []
are used to indicate a set of characters.
\
characters to escape the square brackets inside of the set.This approach also works if you have a multiline string.
import re multiline_string = """[bobby] {hadz} (com)""" new_string = re.sub(r'[()\[\]{}]', '', multiline_string) # bobby # hadz # com print(new_string)
You can adjust the characters between the square brackets if you need to remove different types of brackets.
Here is an example that only removes the square brackets from the string.
import re a_string = '[b]obby' new_string = re.sub(r'[\[\]]', '', a_string) print(new_string) # ๐๏ธ bobby
If you ever need help reading or writing a regular expression, consult the regular expression syntax subheading in the official docs.
The page contains a list of all of the special characters with many useful examples.
You can learn more about the related topics by checking out the following tutorials: