Sigrid
Sigrid

Reputation: 73

Remove quotes around strings

array = format([
    v if v is not None else "*" for v in self._tree.bfs_order_star()
])

The code above returns a string in the following format:

Output: [ 10, 5, 15, '*', '*','*', 20 ]

How can I change it so the *(stars), none-values, are not surrounded by quotes? I tried the following without success.

array = format([
    v if v is not None else "*" for v in self._tree.bfs_order_star()
]).strip('"\'')

Upvotes: 0

Views: 108

Answers (2)

Kelly Bundy
Kelly Bundy

Reputation: 27650

You could create a class/object whose representation actually is just a star:

class Star:
    def __repr__(self):
        return '*'

A demo:

>>> print([1, '*', 2])
[1, '*', 2]
>>> print([1, Star(), 2])
[1, *, 2]

Upvotes: 0

adlopez15
adlopez15

Reputation: 4417

use the replace() method to remove the quotes when printing:

#Convert list to string and replace/remove specific characters.
str(lst).replace("[character to replace/remove]", "[character to replace with or leave empty to remove.]")

Upvotes: 1

Related Questions