Python : xx = "p" + "y" + str(3)
=> xx == "py3"
How can I get the same result using Racket?
(string-append "racket" (number->string 5) " ")
Is there another way in Racket, similar to the Python example above, to append a number to a string?
$ racket
Welcome to Racket v5.3.5.
-> (~a "abc" "def")
"abcdef"
-> (~a "abc" 'xyz 7 )
"abcxyz7"
->
Python automatically coerces the number to a string, while Racket will not do so. Neither Racket nor Python will coerce the number into a string. That is why you must use number->string
explicitly in Racket, and str()
in Python ("p" + str(3)
). You may also find Racket's format
function to behave similarly to some uses of Python's %
operator:
# Python
"py %d %f" % (3, 2.2)
;; Racket
(format "rkt ~a ~a" 3 2.2)
But there is no Racket nor Python equivalent to "foo" + 3
that I know of.
[Answer edited per my mistake. I was confusing Python behavior with JavaScript, misled by OP]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With