[英]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.