text.compare
how different two strings are, the closest match from a list, and line-by-line differences
How different two strings are, and what changed between two texts. The edit distance between two strings is the fewest single-character changes that turn one into the other, where a change is adding a character, removing one, replacing one, or swapping two that are next to each other. “form” and “from” are 1 apart, and “kitten” and “sitting” 3, so it’s a good measure of how likely one is a typo of the other.
Tags: searching
distancethe edit distance: how many single-character changes turn a into bhamminghow many positions two equal-length strings differ at, the Hamming distancesimilarityhow alike two strings are, from 1 (identical) to 0 (nothing in common)closestthe option with the smallest edit distance to s, ignoring upper and lower case, or nil for an empty listdiffwhat changed from text a to b, line by line, or nil if nothing did
Functions
Section titled “Functions”distance
Section titled “distance”distance(a: str, b: str) -> int
the edit distance: how many single-character changes turn a into b
1distance("kitten", "sitting")32distance("form", "from")1See also similarity, closest
hamming
Section titled “hamming”hamming(a: str, b: str) -> int / hamming(a: bytes, b: bytes) -> int
how many positions two equal-length strings differ at, the Hamming distance
Bytes count differing bits instead, the measure for guessing a repeating XOR key’s length
1hamming("karolin", "kathrin")32hamming(b"this is a test", b"wokka wokka!!!")37similarity
Section titled “similarity”similarity(a: str, b: str) -> float
how alike two strings are, from 1 (identical) to 0 (nothing in common)
It’s the edit distance as a share of the longer string’s length, taken from 1
1similarity("color", "colour")0.833333See also distance
closest
Section titled “closest”closest(s: str, options: list) -> any
the option with the smallest edit distance to s, ignoring upper and lower case, or nil for an empty list
1"aple".closest(["apple", "maple", "ape"])"apple"See also distance
diff(a: str, b: str, *, plain: bool = false, bytes: bool = false, table: bool = false, base?: bool|int) -> any
what changed from text a to b, line by line, or nil if nothing did
Changes come in hunks headed @@ with their line numbers, with 3 unchanged lines around them. Removed lines start with - and added lines with +, colored on a terminal with the changed words picked out. With the table flag, it is instead a table with a row per changed line, each with both sides’ line numbers and lines, a removed line next to the one that replaced it and nil on a side that has none
1diff("a\nb\nc", "a\nc\nd")@@ -1,3 +1,3 @@
a
- b
c
+ d2diff("a\nb\nc", "a\nB\nc\nd", table: true)table([{line_a: 2, line_b: 2, old: "b", new: "B"}, {line_a: nil, line_b: 4, old: nil, new: "d"}])As a command:
-p,--plainno colors, even on a terminal-b,--bytesthe command reads the files as bytes, for the bytes diff-t,--tablea table of changed lines instead of hunks-B,--basefor the bytes diff, the address the first byte is at
diff notes.txt notes2.txt# → diff(read_file("notes.txt"), read_file("notes2.txt"))See also distance
More examples
Section titled “More examples”compare
Section titled “compare”1# fix a typo2"recieve".closest(["deceive", "receive", "recipe"])"receive"3# how alike two spellings are4similarity("color", "colour").percent(0)"83%"5# what changed6diff("a\nb\nc", "a\nB\nc\nd")@@ -1,3 +1,4 @@
a
- b
+ B
c
+ dExample results generated on 2026-11-04. Ones using now, today or randomness will differ when you run them.