简体   繁体   中英

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('"\'')

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.]")

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM