<tutorialjinni.com/>

Python convert list to string with brackets

Posted Under: Programming, Python, Tutorials on Nov 6, 2024
Python convert list to string with brackets
To convert a Python list to a string with brackets, you can use the str() function. This method converts the list into a string with brackets included.
my_list = [1, 2, 3, 4]
my_string = str(my_list)
print(my_string)
If you want more control over the formatting, you can use join() along with brackets manually:
my_list = [1, 2, 3, 4]
my_string = "[" + ", ".join(map(str, my_list)) + "]"
print(my_string)
This approach allows you to customize the separator (e.g., ", ") and is helpful for handling lists of different types.


imgae