首页 > 解决方案 > 拆分字符串行代码而不拆分其输出

问题描述

我正在使用 Python 3.7.7。

我有这个代码:

print("Shape:{} - N-Way:{} - Num-shot:{} - Num-query:{} - Epochs:{} - \
        Episodes:{} - Loss_function:'{}' - Accuracy:'{}'".format(image_shape,
        num_way, num_shot, num_query, num_epochs, num_episodes, loss_name,
        accuracy_name))

Epochs:{} - \用来避免行超过 80 个字符。但我得到这个输出:

Shape:(200, 200, 1) - N-Way:2 - Num-shot:5 - Num-query:5 - Epochs:2 -          Episodes:10 - Loss_function:'dice_coef_loss' - Accuracy:'accuracy'

Epochs:2 - Episodes:10 -之间有这个空间。EpochsEpisodes

有没有办法避免呢?我要这个:Epochs:2 - Episodes:10 -

标签: python

解决方案


你可以这样做:

print("Shape:{} - N-Way:{} - Num-shot:{} - Num-query:{} - Epochs:{} - "
      "Episodes:{} - Loss_function:'{}' - Accuracy:'{}'"
      .format(image_shape, num_way, num_shot, num_query, num_epochs,
              num_episodes, loss_name, accuracy_name))

它之所以有效,是因为在 Python 中,如果您有两个仅由空格分隔的字符串文字,则它等效于仅连接这些文字:

>>> 'asd'   'fgh'
'asdfgh'

推荐阅读