python字符串对齐_Python - 字符串对齐
生活随笔
收集整理的這篇文章主要介紹了
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 - 字符串对齐的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jlabel 不能连续两次set_为什么
- 下一篇: java+中的final关键字有哪些用法