Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 38 ยท Syntax and Evaluation

Strings, Characters and Symbols

Learn the text types of Racket and Scheme: strings, characters and symbols. Includes string functions, conversion, comparison and when to use a symbol.

Racket has three text-like types that beginners often mix up. A string is a sequence of characters you can search, join and print. A character is a single letter. A symbol is a name, an atomic label that you compare by identity. Choosing the right one makes programs shorter and faster.

Examples run on the stand-in interpreter. It supports the core string functions shown here; string-join, regular expressions and Unicode details are real Racket features.

Strings

A string is written in double quotes. The usual escapes work: \n newline, \t tab, \" quote, \\ backslash, and ฮป for a Unicode character (ฮป).

RACKET
"hello"
"line one\nline two"
"she said \"hi\""

The most useful string functions:

RACKET
(string-length "racket")          ; 6
(string-append "foo" "bar" "baz") ; "foobarbaz"
(substring "racket" 0 4)          ; "rack"   start inclusive, end exclusive
(string-upcase "abc")             ; "ABC"
(string-downcase "ABC")           ; "abc"
(string=? "a" "a")                ; #t
(string<? "apple" "banana")       ; #t
(string-ref "racket" 0)           ; #\r
Working with strings

String literals are immutable in Racket; functions like string-append return a new string rather than changing the old one.

Building strings from other values

string-append only accepts strings, so convert numbers first. format builds a string using ~a (display style) and ~s (write style):

RACKET
(number->string 42)                       ; "42"
(string->number "3.5")                    ; 3.5
(string->number "oops")                   ; #f
(format "~a is ~a years old" "Ada" 36)    ; "Ada is 36 years old"
(string-join '("a" "b" "c") ", ")         ; "a, b, c"   (racket/string)
(string-split "a b  c")                   ; '("a" "b" "c")

Note that string->number returns #f (not an error) when the text is not a number. Because only #f is false, you can use that result directly in an if.

Conversions and format

Characters

A character is written #\a, #\Z, #\space, #\newline, or #\ฮป. Characters are not strings of length one: they are their own type.

RACKET
#\a
(char->integer #\a)        ; 97
(integer->char 955)        ; #\ฮป
(char-upcase #\a)          ; #\A
(char-alphabetic? #\7)     ; #f
(string->list "abc")       ; '(#\a #\b #\c)
(list->string (list #\h #\i))  ; "hi"

Turning a string into a list of characters lets you use all the list tools. Here is a function that upcases only the first letter:

Characters and lists

Symbols

A symbol is a name. You write one with a quote: 'apple. Two symbols with the same spelling are the same object, so comparing them is fast and exact:

RACKET
'apple                   ; 'apple
(symbol? 'apple)         ; #t
(eq? 'apple 'apple)      ; #t
(symbol->string 'apple)  ; "apple"
(string->symbol "pear")  ; 'pear

Symbols are how Lisp represents identifiers and fixed vocabularies: operation names ('add, 'delete), states ('idle, 'running), field names, and the code itself (since programs are lists of symbols and numbers).

RACKET
(define (describe light)
  (cond [(eq? light 'red)    "stop"]
        [(eq? light 'yellow) "slow down"]
        [(eq? light 'green)  "go"]
        [else "unknown"]))
Symbols as labels

A classic trap is using eq? on strings. It compares object identity, so (eq? (string-append "ap" "ple") "apple") is #f in real Racket even though the text matches. (The stand-in interpreter uses JavaScript strings and cannot show this.) Use equal? or string=? for strings and eq? only for symbols.

Which one to use?

Need Use Example
Text that changes, is searched, or displayed string "Hello"
One letter, one code point character #\a
A fixed label or identifier symbol 'red
Compare quickly by identity symbol with eq? (eq? a 'red)

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does (substring "racket" 0 4) return?

    The start index is inclusive and the end index is exclusive, so positions 0 to 3 give rack.
  2. 2. What does (string->number "oops") return?

    It returns #f when the text is not a number, which lets you test the result with if.
  3. 3. Which comparison is right for symbols?

    Symbols with the same spelling are the same object, so eq? is exact and fast.
  4. 4. What is #\a?

    #\a is the character a, a separate type from strings and symbols.

Summary

  • Strings are text values; most string functions return new strings.
  • Use number->string, string->number and format to convert and build text.
  • Characters are single-letter values written #\a; string->list and list->string connect them to lists.
  • Symbols are names compared with eq?; use them for labels and fixed vocabularies.
  • Compare strings with string=? or equal?, not eq?.

Next: Conditionals: if, cond, case, when and unless