如何用换行符 return 东西? (python)
How to return things with newline? (python)
我现在正在制作一个 flask 项目,我需要 return 来自一个函数的多个值。这是我的代码:
return f"Title: {volume_info['title']}" \
f"Author: {prettify_author}" \
f"Page Count: {volume_info['pageCount']}" \
f"Publication Date: {volume_info['publishedDate']}"
这是我的输出:
output picture
所有内容都在一行中,但我希望所有部分都在不同的行中。有人可以帮忙吗?
如果您的输出是到文件或控制台,您可以使用换行符:
return f"Title: {volume_info['title']}\n" \
f"Author: {prettify_author}\n" \
f"Page Count: {volume_info['pageCount']}\n" \
f"Publication Date: {volume_info['publishedDate']}\n"
如果您要将输出发送到网络浏览器,则需要做一些不同的事情,例如 HTML 换行符:
return f"Title: {volume_info['title']}<br/>" \
f"Author: {prettify_author}<br/>" \
f"Page Count: {volume_info['pageCount']}<br/>" \
f"Publication Date: {volume_info['publishedDate']}<br/>"
您可以使用 triple-quote 字符串来制作多行字符串。三引号之间的所有内容,包括换行符,都包含在字符串中:
def format_data(volume_info, prettify_author):
return f'''Title: {volume_info['title']}
Author: {prettify_author}
Page Count: {volume_info['pageCount']}
Publication Date: {volume_info['publishedDate']}'''
volume_info = {'title': 'Some Title',
'pageCount': 5,
'publishedDate': '01/01/2022'}
print(format_data(volume_info, 'John Doe'))
输出:
Title: Some Title
Author: John Doe
Page Count: 5
Publication Date: 01/01/2022
我现在正在制作一个 flask 项目,我需要 return 来自一个函数的多个值。这是我的代码:
return f"Title: {volume_info['title']}" \
f"Author: {prettify_author}" \
f"Page Count: {volume_info['pageCount']}" \
f"Publication Date: {volume_info['publishedDate']}"
这是我的输出: output picture
所有内容都在一行中,但我希望所有部分都在不同的行中。有人可以帮忙吗?
如果您的输出是到文件或控制台,您可以使用换行符:
return f"Title: {volume_info['title']}\n" \
f"Author: {prettify_author}\n" \
f"Page Count: {volume_info['pageCount']}\n" \
f"Publication Date: {volume_info['publishedDate']}\n"
如果您要将输出发送到网络浏览器,则需要做一些不同的事情,例如 HTML 换行符:
return f"Title: {volume_info['title']}<br/>" \
f"Author: {prettify_author}<br/>" \
f"Page Count: {volume_info['pageCount']}<br/>" \
f"Publication Date: {volume_info['publishedDate']}<br/>"
您可以使用 triple-quote 字符串来制作多行字符串。三引号之间的所有内容,包括换行符,都包含在字符串中:
def format_data(volume_info, prettify_author):
return f'''Title: {volume_info['title']}
Author: {prettify_author}
Page Count: {volume_info['pageCount']}
Publication Date: {volume_info['publishedDate']}'''
volume_info = {'title': 'Some Title',
'pageCount': 5,
'publishedDate': '01/01/2022'}
print(format_data(volume_info, 'John Doe'))
输出:
Title: Some Title
Author: John Doe
Page Count: 5
Publication Date: 01/01/2022