In Emacs Lisp a character is an integer, and a string is an array of characters. There is no separate character type, so (characterp 97) is true and ?a is only another way to write 97. This page covers the character and string syntax that the reader accepts, the main string functions, format, text properties on strings, and the conventions for documentation strings.
Characters
The syntax ?C reads as the code point of C. A character code is at most #x3FFFFF (max-char); codes up to #x10FFFF are Unicode, the rest are raw bytes and other Emacs-specific characters. The bits above 21 are modifier bits (meta, control, shift, hyper, super, alt) and are used by key events, not by text.
| Syntax | Meaning |
|---|---|
?a, ?é | the character itself |
?\n ?\t ?\r ?\f ?\v ?\a ?\b ?\e | newline, tab, return, form feed, vertical tab, bell, backspace, escape |
?\s, ?\d | space; delete (127) |
?\\, ?\(, ?\" | a backslash escapes any other character (parentheses and quotes are better escaped to keep editors happy) |
?\101, ?\x41 | octal (up to three digits) and hexadecimal code |
?\u00e9, ?\U000000e9 | Unicode code point with exactly 4 or 8 hex digits |
?\N{LATIN SMALL LETTER E WITH ACUTE}, ?\N{U+E9} | Unicode character by name or by code point |
?\C-a, ?\^a | control character (code 1); ?\C-% sets the control modifier bit |
?\M-a, ?\S-a, ?\H-a, ?\s-a, ?\A-a | meta, shift, hyper, super and alt modifiers (here \s- with a hyphen is super; \s alone is space) |
Since Emacs 30 a \x that is not followed by hex digits is an error. Predicates and tools: characterp, char-or-string-p, char-uppercase-p, upcase and downcase (which also accept a character), char-width, get-char-code-property, char-to-string, string-to-char, char-from-name and char-to-name (Emacs 30), max-char, unibyte-char-to-multibyte, multibyte-char-to-unibyte.
String syntax
A string is written between double quotes. Newlines are allowed inside. The escapes are those of characters, with these additions and differences:
| Escape | Meaning inside a string |
|---|---|
\n \t \e \\ \" | as for characters |
\ followed by a newline | ignored: lets a long string continue on the next line without a newline |
\ followed by a space | ignored: use it to keep leading spaces on a continued line |
\x41, \101 | hex or octal code; \x41\ ends a hex escape explicitly |
\u00e9, \U000000e9, \N{…} | Unicode escapes; they make the string multibyte |
\C-a, \^I | control characters in the string |
\M-a | the character with bit 7 set (a unibyte value above 127) |
"tab:\t newline:\n"
"A long line that \
continues here"
"caf\u00e9 \N{GREEK SMALL LETTER LAMDA} \x41\ B"
Multibyte and unibyte
A multibyte string holds characters, stored internally as an extension of UTF-8. A unibyte string holds bytes 0 to 255. The reader makes a string multibyte if it contains a non-ASCII character or a \u, \U or \N escape, and unibyte otherwise (octal and hex escapes in the range 128 to 255 give raw bytes). Functions: multibyte-string-p, string-to-multibyte, string-to-unibyte, string-as-unibyte, string-bytes, string-make-multibyte, string-make-unibyte. From Emacs 31, aset on a unibyte string needs a byte value, and on a multibyte string both the old and the new character must be ASCII, so that a string never changes between unibyte and multibyte or in byte length.
String functions
| Area | Functions |
|---|---|
| Make | make-string, string (from characters), concat, substring, substring-no-properties, copy-sequence, string-join, mapconcat |
| Split and trim | split-string (separator regexp, optional omit-nulls and trim), string-trim, string-trim-left, string-trim-right, string-lines, string-chop-newline, string-clean-whitespace (all Emacs 28 except string-trim, which is older) |
| Search and replace | string-search and string-replace (Emacs 28, fixed text), string-prefix-p, string-suffix-p, string-match, replace-regexp-in-string, string-remove-prefix, string-remove-suffix |
| Pad and fit | string-pad, string-limit, string-fill (all Emacs 28), truncate-string-to-width, string-glyph-split and string-pixel-width (Emacs 29) |
| Case | upcase, downcase, capitalize, upcase-initials |
| Compare | string= (alias string-equal), string<, string>, string-lessp, string-collate-lessp, string-equal-ignore-case (Emacs 29), string-version-lessp (digit runs compare as numbers), compare-strings, string-distance (Levenshtein; Emacs 27), string-prefix-p |
| Convert | number-to-string, string-to-number (optional base; returns 0 when there is no number), string-to-list, string-to-vector, char-to-string, string-to-char |
| Measure | length (characters), string-bytes, string-width (columns on screen), char-width, safe-length (for lists) |
| Predicates | stringp, string-empty-p, string-blank-p, string-or-null-p |
string-lines handles a trailing newline differently from Emacs 29 on, as the NEWS for that version states.
format and format-message
(format STRING &rest OBJECTS) returns STRING with each % directive replaced. A directive is %[FIELD$][FLAGS][WIDTH][.PRECISION]CHAR. format-message does the same and additionally converts the quotes `like this' in the format string according to text-quoting-style.
| Directive | Meaning |
|---|---|
%s | the object printed without quotes, as by princ; a precision truncates the text |
%S | the object printed as Lisp, as by prin1, so strings keep their quotes |
%d | decimal integer (a float is truncated) |
%o, %x, %X | octal, lower-case and upper-case hexadecimal integer |
%c | the character with that code |
%e, %f, %g | floating point: exponent form, fixed form, and the shorter of the two |
%% | a literal percent sign |
| Part | Meaning |
|---|---|
N$ | field number: use the Nth argument, so "%2$s %1$s" reorders |
- | left-justify in the width |
0 | pad numbers with zeros |
+, space | always show a sign, or a space instead of a plus |
# | alternate form: 0x for %x, a leading 0 for %o, decimal point for floats |
| width | minimum width (in columns for %s) |
.precision | digits after the point for %f and %e; significant digits for %g; maximum length for %s |
(format "%-6s|%5d|%05.1f|%x" "ab" 42 3.14159 255) ; => "ab | 42|003.1|ff"
(format "%2$s %1$s" "world" "hello") ; => "hello world"
(format "%S" "q") ; => "\"q\""
The binary-as-unsigned variable, an experiment that changed how negative numbers print in %x, was removed in Emacs 31. format-spec (library format-spec) is a separate function that replaces %X keys from an alist and supports the same flags; its SPLIT argument was added in Emacs 28.
Text properties on strings
A string can carry properties for ranges of characters. The printed and read form is #("hello world" 0 5 (face bold)): the text, then triples of start, end and a property list.
| Function | Meaning |
|---|---|
propertize | a copy of a string with properties for the whole text |
put-text-property, add-text-properties, set-text-properties, remove-text-properties | change a range (in a string, the object is the last argument) |
get-text-property, text-properties-at, next-single-property-change | read and scan |
substring-no-properties, buffer-substring-no-properties | copy without properties |
The standard properties and the buffer side are on Buffers and text.
Documentation strings
A documentation string is the optional string after the arguments of a defun or after the value of a defvar. Conventions in the manual: the first line is a complete sentence that summarises the object, because apropos shows only that line; arguments are named in capitals; the string uses the quoting `like this' or the curved quotes directly, and the help system converts them.
| Markup | Replaced by |
|---|---|
\[command] | the key sequence that runs the command in the current keymaps, or M-x command |
\{keymap} | a table of all the bindings in that keymap variable |
\<keymap> | no text; it selects the keymap that later \[…] look in |
\= | quotes the next character, so \=\[ stays as it is |
| `like this' or ‘like this’ | the quotes in the style of text-quoting-style |
substitute-command-keys does the conversion. In source code the backslash in the markup must be doubled ("\\[forward-char]"). The variable text-quoting-style is curve, straight or grave; nil picks the best that the display supports. Emacs 29 added a byte-compiler warning for some quoting mistakes in docstrings, and since Emacs 30 a string returned by a function or macro is never taken as a docstring.
Coding systems
encode-coding-string turns a string into a unibyte string in a coding system (utf-8, iso-8859-1, utf-16le, …) and decode-coding-string does the reverse. Related: coding-system-p, detect-coding-string, check-coding-system, encode-coding-region, decode-coding-region, find-coding-systems-string. The line-ending variants are the suffixes -unix, -dos and -mac. The coding system of a source file is set by the coding: cookie (File structure).
Not described
Known, not yet covered
- Reference: printing and reading of objects (
prin1,princ,print-escape-newlines,read-from-string) and the full set ofprint-variables. - Reference: character sets, charsets, input methods, character properties from the Unicode database and the bidirectional text functions.
- Reference: the complete list of coding systems and the rules of
auto-coding-alist. - Reference: the extended
formatrules for non-ASCII text in widths, and for%cwith raw bytes.
See also
- Lexical structure – how the reader recognises character and string literals
- Regexps and syntax tables –
string-matchandreplace-regexp-in-string - Buffers and text – text properties in buffers
- File structure – encodings and the
codingcookie - Strings and Characters – the manual’s chapter
- Tips for Documentation Strings – the manual’s conventions