现在的位置: 首页 > 综合 > 正文

001_014 Python 改变多行文本字符串的缩进

2018年02月15日 ⁄ 综合 ⁄ 共 225字 ⁄ 字号 评论关闭

代码如下:

#encoding=utf-8
print '中国'

#改变多行文本字符串的缩进

def reindent(s,numSpaces):
    leading_space = numSpaces * ' '
    lines = [leading_space + line.strip() for line in s.splitlines()]
    return '\n'.join(lines)
print reindent("123\n456\n中国\n",2)

打印结果如下:

中国
  123
  456
  中国

抱歉!评论已关闭.