I am creating a string using f string and need to avoid fields which are None.
I am currently using below code
def output(self) -> str:
data = ''
data += 'Books: [\n'
for bookid, firstname, lastname in zip(self.book_id, self.first_name, self.last_name):
data += f'{{book_id: "{bookid}", first_name: "{firstname}",last_name : "{lastname}"}},\n'
data = data [:-2] + '\n'
data += ']\n'
return data
I get below output:
Books: [ { book_id: "123", first_name: "John", last_name: "None" }, { book_id: "567", first_name: "Merry", last_name: "Jones" } ]
How to ensure that None fields should not be part of the string in the for loop. I want the output as below:
Books: [ { book_id: "123", first_name: "John"}, { book_id: "567", first_name: "Merry", last_name: "Jones"} ]
Using below I am able to get the desired output: