python中实现字符串的排版可以使用str.ljust(), str.rjust(), str.center()这三个函数,具体的用法参见下面的代码。
<span style="font-size:18px;">#!/bin/python
# -*- coding=utf8 -*-
print """任务:实现字符串对齐(左对齐、右对齐、居中)"""
print '|','hej'.ljust(20),'|','hej'.rjust(20),'|','hej'.center(20)
print '|','hej'.ljust(20,'*'),'|','hej'.rjust(20,'+'),'|','hej'.center(20,'#')</span>