-
Notifications
You must be signed in to change notification settings - Fork 0
python Namespace
Derek Snider edited this page May 19, 2026
·
1 revision
16 Python-style string operations: title case, case swapping, alignment, zero-padding, character class testing, substring counting, and format() with {} placeholders.
See also: Namespaces Overview | Other namespaces
| Function | Description | Example |
|---|---|---|
title(str) |
Title Case every word |
python::title(s) — "hello world" → "Hello World" |
swapcase(str) |
Swap upper/lower case |
python::swapcase(s) — "Hello" → "hELLO" |
| Function | Description | Example |
|---|---|---|
center(str, width, fill) |
Center with fill char | python::center(s, 20, "-") |
ljust(str, width, fill) |
Left-justify, pad right | python::ljust(s, 20, ".") |
rjust(str, width, fill) |
Right-justify, pad left | python::rjust(s, 20, ".") |
zfill(str, width) |
Zero-pad numeric string |
python::zfill(s, 8) — "42" → "00000042" |
| Function | Description | Example |
|---|---|---|
count(str, substr) |
Count non-overlapping occurrences | n = python::count(s, "an") |
startswith(str, prefix) |
Check prefix (returns 0/1) | if ( python::startswith(s, "http") ) |
endswith(str, suffix) |
Check suffix (returns 0/1) | if ( python::endswith(s, ".txt") ) |
| Function | Description | Example |
|---|---|---|
isdigit(str) |
All digits? | if ( python::isdigit(s) ) |
isalpha(str) |
All alphabetic? | if ( python::isalpha(s) ) |
isalnum(str) |
All alphanumeric? | if ( python::isalnum(s) ) |
isspace(str) |
All whitespace? | if ( python::isspace(s) ) |
| Function | Description | Example |
|---|---|---|
replace(str, old, new) |
Replace all occurrences | python::replace(s, "foo", "bar") |
array args;
php::array_push(args, "World");
php::array_push_int(args, 42);
string fmt = "Hello {}, the answer is {}";
string result;
python::format(result, fmt, args);
// result: "Hello World, the answer is 42"format() takes a format string with {} placeholders and a array of values.
#include <iostream>
int main()
{
string s = "hello world";
python::title(s);
cout << s << endl; // Hello World
s = "42";
python::zfill(s, 8);
cout << s << endl; // 00000042
s = "banana";
string sub = "an";
int n;
n = python::count(s, sub);
cout << n << endl; // 2
return 0;
}