Python:xx = "p" + "y" + str(3)
=>xx == "py3"
如何使用球拍獲得相同的結果?如何在球拍中添加一個數字到字符串?
(string-append "racket" (number->string 5) " ")
是否有球拍另一種方式,類似於Python的例子以上,多項附加到一個字符串?
Python:xx = "p" + "y" + str(3)
=>xx == "py3"
如何使用球拍獲得相同的結果?如何在球拍中添加一個數字到字符串?
(string-append "racket" (number->string 5) " ")
是否有球拍另一種方式,類似於Python的例子以上,多項附加到一個字符串?
Python自動將數字強制轉換爲一個字符串,而Racket將不是這樣做。球拍和Python都不會將數字強制轉換爲字符串。這就是爲什麼你必須在球拍中明確使用number->string
,在Python("p" + str(3)
)中必須使用和str()
。您也可以找到球拍的format
功能類似的行爲Python的%
運營商的一些用法:
# Python
"py %d %f" % (3, 2.2)
;; Racket
(format "rkt ~a ~a" 3 2.2)
但沒有球拍相當於"foo" + 3
,我知道也不是Python的。
[編輯按我的錯誤回答。我將Python的行爲與JavaScript混淆,被OP誤導]
Python不會自動將數字強制轉換爲字符串。該OP是不正確的。 –
@MarkTolonen:* facepalm *正確的你。 –
$ racket
Welcome to Racket v5.3.5.
-> (~a "abc" "def")
"abcdef"
-> (~a "abc" 'xyz 7)
"abcxyz7"
->
你試過你的Python例子嗎? –
我嘗試:TypeError:不能連接'str'和'int'對象。在這個問題之前,python auto將數字強制轉換爲我心中的字符串 –