[英]how can I output ' ' with using print in python
print('{0:^10}'.format('hi')) output - hi
'{0:^10}'.format('hi') output -' hi '
我可以使用print
功能輸出第二個句子中的第一個句子嗎?
是的,你只需要逃避你單身'
通過把一個反斜杠小號\\
在他們面前-這被稱為串逃逸,幫助您print
功能注意到,這些字符應顯示為純文本,並不能解釋像往常一樣,“特字符。
或只用雙引號引起來
print("'{0:^10}'".format("hi"))
您可以使用repr
,它對於字符串返回用於創建字符串的文字的表示形式:
>>> print(repr('{0:^10}'.format('hi')))
' hi '
還有其他方法可以使用反斜杠,也可以不使用反斜杠:
>>> print('\'{0:^10}\''.format('hi'))
' hi '
>>> print("'{0:^10}'".format('hi'))
' hi '
但是我認為使用repr
是最清晰,更通用的方法。
無需repr
>>> print('\'{0:^10}\''.format('hi'))
' hi '
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.