PHP trim() Equivalent in Python

Python's str.strip() does the same job as PHP's trim(), stripping leading and trailing whitespace by default, but the two languages' default whitespace character sets aren't perfectly identical.

Close equivalent Covers the common case, but has documented behavioral differences.

PHP source
trim()
Closest Python API
str.strip()

Main limitation: PHP's `trim()` default character set is `" \t\n\r\0\x0B"` (space, tab, newline, carriage return, NUL byte, vertical tab); Python's `str.strip()` uses Unicode whitespace (via `str.isspace()`), which includes more characters (e.g. various Unicode space separators) but notably does *not* strip a NUL byte (`\0`) by default.

How to use each function

Before comparing the two, here's how trim() and str.strip() work on their own.

PHP: trim()

trim(string $string, string $characters = " \t\n\r\0\x0B"): string

Strips characters from both the start and end of a string. With no second argument it removes the default whitespace set; passing $characters replaces that set entirely and supports range shorthand like 'a..z'. ltrim()/rtrim() strip only one side.

Returns: The trimmed string.

PHP
trim("  hello  "); // "hello"
trim("--hello--", "-"); // "hello"

Python: str.strip()

str.strip(chars=None)

Removes leading and trailing characters from a string. With no argument it strips Unicode whitespace; passing chars removes any character in that set (not a literal prefix/suffix) from both ends. str.lstrip()/str.rstrip() trim only one side.

Returns: The trimmed string.

Python
"  hello  ".strip()      # 'hello'
"--hello--".strip("-")   # 'hello'

Quick mapping

PHP trim()
Closest Python API str.strip()

How it differs

  • PHP's trim() default character set is " \t\n\r\0\x0B" (space, tab, newline, carriage return, NUL byte, vertical tab); Python's str.strip() uses Unicode whitespace (via str.isspace()), which includes more characters (e.g. various Unicode space separators) but notably does *not* strip a NUL byte (\0) by default.
  • PHP's trim($str, $charlist) second argument is a literal character list (supports ranges like "a..z"); Python's str.strip(chars) argument is also a set of characters to strip, with the same "any character in this set" semantics — but Python has no range shorthand, so "a..z" must be written out or generated (e.g. string.ascii_lowercase).
  • PHP has separate ltrim()/rtrim() for one-sided trimming; Python's equivalents are str.lstrip()/str.rstrip() — the naming pattern maps directly (left/right) once you know Python drops the leading l/r before a different verb.

Examples

Basic whitespace trim

PHP
trim("  hello  ");
// "hello"
Python
"  hello  ".strip()
# 'hello'
Both strip leading and trailing whitespace by default, with matching results for ordinary space/tab/newline characters.

Trimming a custom character set

PHP
trim("--hello--", "-");
// "hello"
Python
"--hello--".strip("-")
# 'hello'
Both accept an explicit set of characters to strip instead of whitespace.

Edge cases

Edge case

If your PHP code relies on trim() removing a NUL byte (\0) — historically relevant for security-sensitive string handling — Python's str.strip() will *not* remove it by default; pass \x00 explicitly in the chars argument if that matters.

References

  • PHP Manual — trim — checked 2026-07-29
  • Python docs — str.strip() (v3.x) — checked 2026-07-29

Last verified 2026-07-29.