I have a list in Python e.g.
names = ["Sam", "Peter", "James", "Julian", "Ann"]
I want to print the array in a single line without the normal " []
names = ["Sam", "Peter", "James", "Julian", "Ann"]
print (names)
Will give the output as;
["Sam", "Peter", "James", "Julian", "Ann"]
That is not the format I want instead I want it to be like this;
Sam, Peter, James, Julian, Ann
Note: It must be in a single row.
use join() function to print array or without square brackets or commas. The join() function takes an iterable object such as a list, tuple, dictionary, string, or set as an argument and returns a string in which all the elements are joined by a character specified with the function.
Using strip() to Remove Brackets from the Beginning and End of Strings in Python. If your brackets are on the beginning and end of your string, you can also use the strip() function. The Python strip() function removes specified characters from the beginning and end of a string.
To print a NumPy array without enclosing square brackets, the most Pythonic way is to unpack all array values into the print() function and use the sep=', ' argument to separate the array elements with a comma and a space.
Using join function to remove brackets from a list in Python. join() is a built-in function in python. This method takes all the elements from a given sequence. Then it joins all the elements and prints them into a single element.
print(', '.join(names))
This, like it sounds, just takes all the elements of the list and joins them with ', '
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With