How do you remove code indentation from a prompt while keeping nested detail?
dedent removes shared leading whitespace from nonblank lines while retaining deeper indentation.
Python example and interpretation
Lines indented four and six spaces retain a two-space difference after cleanup. Mixed tabs and spaces can look aligned without sharing an identical prefix.
from textwrap import dedent
dedent(' Task\n Detail').splitlines()Result ['Task', ' Detail']
中文
如何去除提示词模板的代码缩进而保留相对层次?
dedent 去掉各非空行共同的前导空白,保留超过共同缩进的部分。
Python 算例与适用条件
第一行和第二行分别缩进四格与六格,清理后还差两格。混用制表符与空格时即使视觉对齐,共同前缀也可能不同。
from textwrap import dedent
dedent(' Task\n Detail').splitlines()结果 ['Task', ' Detail']