所以我想弄清楚如何我可以使一個函數作爲輸入一個字符序列,沒有定義爲一個字符串,並讓它自動解釋作爲實例的字符串:我如何使一個函數將其參數轉換爲字符串
def printstring(sequence_of_characters):
a=str(sequence_of_characters)
print(a)
現在,當我運行:
printstring(hello)
我希望端子輸出:
'hello'
相反,它輸出
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
printstring(hello)
NameError: name 'hello' is not defined
現在我意識到,我可以叫PRINTSTRING的「你好」,但我希望函數能夠採取的輸入,是不是已經是一個字符串,而僅僅是一個序列chracters。
U應該調用打印字符串(「hello」) – Srinath 2014-11-06 00:01:40
@Srinath說什麼,再加上你可以'print(sequence_of_characters)'。 – martineau 2014-11-06 00:04:03
根據定義,字符序列是一個字符串。 – etheranger 2014-11-06 00:07:24