Skip to content

String Methods

String methods are pure. They return new values and never mutate the receiver.

Every position and width is measured in Unicode code points. This includes start and end bounds, indices returned by search methods, alignment widths, and empty-substring boundary counts.

| Method | Returns | | ---------------- | ------------------------------------------------------------------ | | s.capitalize() | string with the first character uppercased and the rest lowercased | | s.casefold() | Unicode 16.0 default case-folded string | | s.lower() | lowercase string | | s.upper() | uppercase string | | s.title() | title-cased string | | s.swapcase() | string with uppercase and lowercase characters swapped |

| Method | Returns | | ---------------------------- | ------------------------------------------------------- | | s.strip() | string with surrounding whitespace removed | | s.strip(chars) | string with surrounding characters from chars removed | | s.lstrip() | string with leading whitespace removed | | s.lstrip(chars) | string with leading characters from chars removed | | s.rstrip() | string with trailing whitespace removed | | s.rstrip(chars) | string with trailing characters from chars removed | | s.replace(old, new) | string with every old replaced by new | | s.replace(old, new, count) | string with at most count replacements | | s.removeprefix(prefix) | string without prefix when present | | s.removesuffix(suffix) | string without suffix when present |

| Method | Returns | | --------------------------- | --------------------------------------------- | | s.center(width) | centered string padded with spaces | | s.center(width, fillchar) | centered string padded with fillchar | | s.ljust(width) | left-justified string padded with spaces | | s.ljust(width, fillchar) | left-justified string padded with fillchar | | s.rjust(width) | right-justified string padded with spaces | | s.rjust(width, fillchar) | right-justified string padded with fillchar | | s.zfill(width) | string left-padded with zeroes |

width must be an integer. fillchar must be a one-character string.

| Method | Returns | | ------------------ | -------------------------------------------------------------- | | s.isalnum() | whether s is non-empty and all letters or numbers | | s.isalpha() | whether s is non-empty and all letters | | s.isascii() | whether s contains only ASCII characters | | s.isdecimal() | whether every character has Unicode Numeric_Type=Decimal | | s.isdigit() | whether every character has Numeric_Type=Decimal or Digit | | s.isidentifier() | whether s matches a Nilakan identifier shape | | s.islower() | whether cased characters are lowercase | | s.isnumeric() | whether every character has any Unicode numeric type | | s.isprintable() | whether every character is printable, with ASCII space allowed | | s.isspace() | whether s is non-empty and all whitespace | | s.istitle() | whether s is title-cased | | s.isupper() | whether cased characters are uppercase |

isidentifier() checks Nilakan’s lexical identifier shape: an ASCII letter or underscore followed by zero or more ASCII letters, digits, or underscores. It does not test whether the name is available in a particular scope. Reserved words such as while therefore return True. Unicode letters and primed spellings such as x' return False because neither is a lexical Nilakan identifier token.

| Method | Returns | | ---------------------------------- | ------------------------------------------- | | s.startswith(prefix) | whether s starts with prefix | | s.startswith(prefix, start) | whether s[start:] starts with prefix | | s.startswith(prefix, start, end) | whether s[start:end] starts with prefix | | s.endswith(suffix) | whether s ends with suffix | | s.endswith(suffix, start) | whether s[start:] ends with suffix | | s.endswith(suffix, start, end) | whether s[start:end] ends with suffix | | s.count(sub) | occurrence count | | s.count(sub, start) | occurrence count in s[start:] | | s.count(sub, start, end) | occurrence count in s[start:end] | | s.find(sub) | first index, or -1 | | s.find(sub, start) | first index in s[start:], or -1 | | s.find(sub, start, end) | first index in s[start:end], or -1 | | s.rfind(sub) | last index, or -1 | | s.rfind(sub, start) | last index in s[start:], or -1 | | s.rfind(sub, start, end) | last index in s[start:end], or -1 | | s.index(sub) | first index | | s.index(sub, start) | first index in s[start:] | | s.index(sub, start, end) | first index in s[start:end] | | s.rindex(sub) | last index | | s.rindex(sub, start) | last index in s[start:] | | s.rindex(sub, start, end) | last index in s[start:end] | | s.get(index) | character, or IndexError/NLK4004 |

The return type of index() and rindex() is num | err; an absent substring produces ValueError/NLK4002. The return type of get() is str | err.

Negative start and end bounds are normalized from the end. Nilakan then clamps both bounds to 0..len(s). This is an intentional Nilakan rule and differs from Python for an empty substring when start is above the string length: "abc".find("", 4) is 3 in Nilakan because 4 clamps to 3. find("") returns 0, rfind("") returns the string length, and count("") counts boundary positions in the normalized slice.

Raw string indexing is fatal on a miss. get() is the safe value-level alternative.

| Method | Returns | | ------------------------- | ----------------------------------------------------------- | | sep.join(iterable) | string joining a string, list, or tuple of strings | | s.split() | list split on whitespace | | s.split(sep) | list split on sep | | s.split(sep, maxsplit) | list split at most maxsplit times | | s.rsplit() | list split from the right on whitespace | | s.rsplit(sep) | list split from the right on sep | | s.rsplit(sep, maxsplit) | list split from the right at most maxsplit times | | s.splitlines() | list of lines without line breaks | | s.splitlines(keepends) | list of lines, keeping breaks when keepends is True | | s.partition(sep) | tuple of before, separator, after | | s.rpartition(sep) | tuple of before, separator, after, searching from the right |

The forms with an explicit separator return list | err: split() and rsplit() produce ValueError/NLK4002 for an empty separator. partition() and rpartition() return tuple[str, str, str] | err and produce the same error for an empty separator. maxsplit must be an integer. partition() returns (s, "", "") when a non-empty separator is absent. rpartition() returns ("", "", s) when it is absent.

Predicate methods that require content return False on an empty string. "".isascii() and "".isprintable() return True.

The three numeric predicates are deliberately different. For example, superscript two ("²") is a digit but not a decimal character, vulgar fraction one quarter ("¼") is numeric but not a digit, and the ideograph "一" is numeric. isprintable() rejects line separators, controls, format characters such as the byte-order mark, and all other Unicode separator/Other categories except ordinary ASCII space. isspace() includes Unicode whitespace and the historical U+001C–U+001F separators, but not U+FEFF.

String methods require a string receiver. Method arguments are checked by method: substring, separator, prefix, suffix, and chars parameters must be strings; width, count, start, end, and maxsplit must be integers; keepends must be boolean. Those type and arity failures are fatal diagnostics.

The two-argument center, ljust, and rjust forms return str | err because a fillchar with anything other than one Unicode code point produces ValueError/NLK4002. One-argument alignment cannot produce that value error.

name = " nilakan "
print(name.strip().upper())
parts = "a,b,c".split(",")
print("-".join(parts))
head, sep, tail = "key=value".partition("=")
print(head)
print(tail)
print("abcdef".find("de", -3))
print("key".partition("="))
print("".isprintable())