Python 字符填充问题

本文介绍如何使用Python进行格式化输出,包括使用自定义填充符号、指定宽度和对齐方式等技巧,通过具体代码示例展示了左对齐、居中和右对齐的效果。

在用Python输出时,希望和C语言里面一样,达到以下效果

  1. 使用自定义填充符号。
  2. 以左、居中、右的方式输出。
  3. 以指定宽度输出指定字符串。

代码如下:
#!/usr/bin/python

import codecs
import string

def print_align_string():
        test_string = [
                "Chinese",
                "Python",
                "Welcome to python word"
        ];
        for str in test_string:
                print '{0:->30}'.format(str);
        print
        for str in test_string:
                print '{0: <40}'.format(str);
        print
        for str in test_string:
                print '{0:*^50}'.format(str);
        print
        print string.center("Another Way",60,"#")
        print
        for str in test_string:
                print string.rjust(str,30,"-");
        print
        for str in test_string:
                print string.ljust(str,40," ");
        print
        for str in test_string:
                print string.center(str,50,"*");

if __name__ == "__main__":
        print_align_string();

运行效果如下:
-----------------------Chinese
------------------------Python
--------Welcome to python word

Chinese                                 
Python                                  
Welcome to python word                  

*********************Chinese**********************
**********************Python**********************
**************Welcome to python word**************

########################Another Way#########################

-----------------------Chinese
------------------------Python
--------Welcome to python word

Chinese                                 
Python                                  
Welcome to python word                  

*********************Chinese**********************
**********************Python**********************
**************Welcome to python word**************




Python提供了多种字符填充的方法,以下是一些常见的方式: ### `zfill()` 方法 `zfill()` 方法用于在字符串的左侧填充零,直到字符串达到指定的长度。该方法会根据字符串的正负号和内容进行不同的处理,对于纯数字字符串,会在前面填充零;对于带有正负号的数字字符串,会越过正负号填充零;对于包含其他符号的字符串,也会在前面填充零 [^1]。 ```python # 不加"+""-"纯数字,用填充物"0"将字符串前填充满 print("12345".zfill(10)) # 加"-"纯数字,越过"-"用填充物"0"将字符串前填充满 print("-125".zfill(10)) # 加"+"数字字母组合,越过"+"用填充物"0"将字符串前填充满 print("+qwe125".zfill(10)) # 加其他符号,用填充物"0"将字符串前填充满 print("#qwe12".zfill(10)) ``` ### `format()` 函数 `format()` 函数可以用来把值插入到字符串中的花括号 `{}` 中。如果需要填充空格,则可以使用空格和冒号来控制填充的宽度 [^2]。 ```python # 用空格填充字符串 print("{:10}".format("hello")) ``` ### `center()`、`ljust()` 和 `rjust()` 方法 - `center()` 方法将字符串居中,并使用指定的字符(默认为空格)填充到指定的长度 [^4]。 - `ljust()` 方法将字符串左对齐,并使用指定的字符(默认为空格)填充到指定的长度。 - `rjust()` 方法将字符串右对齐,并使用指定的字符(默认为空格)填充到指定的长度。 ```python str1 = "今天天气好晴朗" # 使用空格将原字符填充到50个长度,原内容居中 print(str1.center(50)) # 使用 * 将原字符填充到50个长度,原内容居中 print(str1.center(50, "*")) # 左对齐,用空格填充到指定长度 print(str1.ljust(50)) # 右对齐,用空格填充到指定长度 print(str1.rjust(50)) ``` ### f-string 格式化(Python 3.6+) f-string 是 Python 3.6 及以上版本引入的一种字符串格式化方法,可以在字符串前加上 `f` 或 `F`,并在字符串中使用花括号 `{}` 来引用变量。同样可以使用冒号和填充字符来控制填充 [^3]。 ```python text = "example" # 左对齐,用空格填充到15个字符长度 print(f"{text:<15}") # 右对齐,用 * 填充到15个字符长度 print(f"{text:*>15}") # 居中,用 - 填充到15个字符长度 print(f"{text:-^15}") ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值