在Python解释器中,返回时不带" '" [英] In Python interpreter, return without " ' "

查看:92
本文介绍了在Python解释器中,返回时不带" '"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python中,如何返回类似这样的变量:

In Python, how do you return a variable like:

function(x):
   return x

x周围没有'x'(')吗?

推荐答案

在Python交互式提示中,如果您返回一个字符串,则会显示 并带有引号,主要是让您知道这是一个字符串.

In the Python interactive prompt, if you return a string, it will be displayed with quotes around it, mainly so that you know it's a string.

如果只是打印字符串,则不会显示带引号的字符串(除非字符串中带有的引号).

If you just print the string, it will not be shown with quotes (unless the string has quotes in it).

>>> 1 # just a number, so no quotes
1
>>> "hi" # just a string, displayed with quotes
'hi'
>>> print("hi") # being *printed* to the screen, so do not show quotes
hi
>>> "'hello'" # string with embedded single quotes
"'hello'"
>>> print("'hello'") # *printing* a string with embedded single quotes
'hello'

如果实际上要做需要删除前导/结尾引号,请使用字符串的.strip方法删除单引号和/或双引号:

If you actually do need to remove leading/trailing quotation marks, use the .strip method of the string to remove single and/or double quotes:

>>> print("""'"hello"'""")
'"hello"'
>>> print("""'"hello"'""".strip('"\''))
hello

这篇关于在Python解释器中,返回时不带" '"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆