faqts : Computers : Programming : Languages : Python : Snippets : Strings

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

1 of 1 people (100%) answered Yes

Entry

Indent triple-quoted strings

Jul 5th, 2000 10:03
Nathan Wallace, unknown unknown, Hans Nowak, Snippet 378, Michael Wallace


"""
Packages: text.docstrings;basic_datatypes.strings
"""
"""
Hey all,
  I posted a couple days ago about actually needing a whitespace-eating
nanovirus.. And ever since I built it, I've been using it almost every
day for text processing... Being able to indent triple-quoted-string
is a really really nice feature:
"""
import string
def trim(s):
    """strips leading indentation from a multi-line string."""
    lines = string.split(s, "\n")
    # strip leading blank line
    if lines[0] == "":
        lines = lines[1:]
    # strip indentation
    indent = len(lines[0]) - len(string.lstrip(lines[0]))
    for i in range(len(lines)):
        lines[i] = lines[i][indent:]
    return string.join(lines, "\n")
"""
Anyway, I was wondering if anyone else ever has need of that particular
capability. I think it's useful enough to go into the string module, and
I was just debating whether or not to submit it...  here's the code:
"""