12

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?

Anderson Green
  • 30,230
  • 67
  • 195
  • 328
book-life
  • 149
  • 1
  • 5

2 Answers2

13
$ racket
Welcome to Racket v5.3.5.
-> (~a "abc" "def")
"abcdef"
-> (~a "abc" 'xyz 7 )
"abcxyz7"
-> 
alinsoar
  • 15,386
  • 4
  • 57
  • 74
11

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]

Dan Burton
  • 53,238
  • 27
  • 117
  • 198