Crispy13
Crispy13

Reputation: 369

Is there any way to fill empty string into {} if the variable of {} does not exist?

# Evaluation result.
train_eval_results = model.evaluate(x_train, [y_train, x_train])
val_eval_results = model.evaluate(x_val, [y_val, x_val])
batch_size = 2
tc = TimeChecker()

### model is from Keras package.
### val_eval_results is a list of 5 float numbers.

content = f"""
train_evaluation_result = {train_eval_results[4]:.4f}
val_evaluation_result = {val_eval_results[4]:.4f}
elapsed_time = {tc.show_elapsed_time()} 
batch_size = {batch_size}
"""

### # show_elapsed_time() return a string.

I want to make a readme file which includes the above content. But sometimes part of the variables in {} do not exist. In that situation, i want to fill empty string or "not defined" thing into {}.

Could you help me?

Upvotes: 2

Views: 268

Answers (1)

Steven
Steven

Reputation: 609

I Believe you can use the inline conditional:

f"batch_size = {batch_size if batch_size else ''}"

Upvotes: 5

Related Questions