Pythonでは、次のような変数をどのように返しますか。
function(x):
return x
'x'
('
)がx
?の周りにない場合
Pythonでは、次のような変数をどのように返しますか。
function(x):
return x
'x'
('
)がx
?の周りにない場合
Pythonインタラクティブプロンプトでは、文字列を返すと、主に文字列であることがわかるように、文字列を引用符で囲んで表示されます。
文字列を印刷するだけでは、引用符は表示されません(文字列に引用符が含まれている場合を除く)。
>>> 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
>>> print("""'"hello"'""")
'"hello"'
>>> print("""'"hello"'""".strip('"\''))
hello
文字列内のすべての一重引用符を削除する1つの方法があります。
def remove(x):
return x.replace("'", "")
最初と最後の文字を削除する別の方法があります。
def remove2(x):
return x[1:-1]