Library contents

Python str.lstrip

Remove leading spaces while preserving spacing at the other edge.

A simple example

str.lstrip

  1. Start with " A "
  2. Do this Remove leading spaces
  3. Result A then two spaces
Only the spaces before A disappear.
lesson.py
value = "  A  "
print("[" + value.lstrip() + "]")
Output
[A  ]

Syntax

Python syntax
"[" + value.lstrip() + "]"
Run these examples in Python

Save one example at a time as lesson.py. Use Python 3.9 or later.

python3 lesson.py

Align a pasted seat label

A user pastes a multi-line block from a formatted document into a plain-text field. Remove the common outer margin while preserving meaningful differences between the lines inside it.

From the programvalue = " A-17 "
Result[A-17 ]
lesson.py
value = "  A-17 "
print("[" + value.lstrip() + "]")
Output
[A-17 ]

The leading spaces vanish and the reserved space after 17 remains.

Advertisements

Clean a copied delivery field

A user pastes a name or code from another application. Remove the unwanted outer padding while preserving the spaces and letters that belong to the actual value.

From the programvalue = "\tNorth Road"
Result[North Road]
lesson.py
value = "\tNorth Road"
print("[" + value.lstrip() + "]")
Output
[North Road]

The tab at the beginning is removed.

Clean a course import

A course name arrives padded on both sides. Remove only the leading spaces. Brackets make any spaces left at the end visible.

Given' Art '
Worked result[Art ]

What to watch for

The brackets in the output make significant trailing spaces visible.

Documentation for str.lstrip