天天看點

python字元串對齊_Python - 字元串對齊

字元串對齊

本文位址: http://blog.csdn.net/caroline_wendy/article/details/20463231

Python中, 字元串對齊, 使用ljust(), 左對齊;rjust(), 右對齊; center(), 中間對齊;

也可以修改第三個參數, 修改填充資料, 預設使用空格;

代碼如下:

# -*- coding: utf-8 -*- #==================== #File: TextExercise.py #Author: Wendy #Date: 2014-03-04 #==================== #eclipse pydev, python2.7 print('|' + 'hej'.ljust(20) + '|' + 'hej'.rjust(20) + '|' + 'hej'.center(20) + '|') print('hej'.center(20, '+')) #一共有20個字元, 中間使用hej, 其他用+填充

輸出:

|hej | hej| hej | ++++++++hej+++++++++

python字元串對齊_Python - 字元串對齊