python print 输出对齐 包含中文无法对齐

首先,先简单介绍一下,print自己有对齐的方式

下面的,就是给定字符长度10个字符长度,对齐方式为左边对齐,空余的字符采用 * 填充

string = "1234567"
print(string.ljust(10,"*"))

当然了,我整理了三个方式,左右中,一起打印出来了

string = "1234567"
print(string.ljust(10,"*"))#左
print(string.center(10,"*"))#中
print(string.rjust(10,"*"))#右

综上就会有一个不错的打印结果如下:

print("\n\n")
print("Name".ljust(10," "),end="")
print("Price".ljust(10," "),end="")
print("")
print("Noodle".ljust(10," "),end="")
print("10RMB".ljust(10," "),end="")
print("")
print("Meat".ljust(10," "),end="")
print("40RMB".ljust(10," "),end="")
print("\n\n")

但是当你将字符换为中文的时候,你就会发现它的结果就不那么尽人意了

print("\n\n")
print("名字".ljust(10," "),end="")
print("价格".ljust(10," "),end="")
print("")
print("面条".ljust(10," "),end="")
print("10元".ljust(10," "),end="")
print("")
print("肉".ljust(10," "),end="")
print("40元".ljust(10," "),end="")
print("\n\n")

原因很简单,因为中文字符默认是全角输出,而英文字符默认为半角, 半角和全角就像英文的  "," 和中文的 "," ,中文的逗号后面会更出一个小空格来,所以造成了这样的不规则

解决方法:

将非半角全部转换为半角,话不多说函数如下,调用方法我写在函数的最顶端了

'''
调用方法
#左边对齐
string1 = "字符串123"
result = duiqi(string1,10,"left")#对齐字符串,长度,对齐方式
print(result)
#右边对齐
string1 = "字符串123"
result = duiqi(string1,10,"right")#对齐字符串,长度,对齐方式
print(result)
#居中对齐这个有点麻烦,例如 “123” ,对齐长度 6 ,要补三个空格,前面一个后面两个还是前面两个后面一个,这个是可以设置的
对齐的方式有 center0 center1 center2
center1:前面的空格更少,后面的更多
center2:后面的空格更少,前面的更多
center0:两边空格一样多(需要字符合理——例: 字符串“123”, 长度为 “5”,不然要报错)
'''
def duiqi(string,length,way):
    if(way == "left"):
        difference = length - len(string)  # 计算限定长度时需要补齐多少个空格
        if difference == 0:  # 若差值为0则不需要补
            return string
        elif difference < 0:
            print('错误:限定的对齐长度小于字符串长度!')
            return None
        new_string = ''
        space = ' '
        for i in string:
            codes = ord(i)  # 将字符转为ASCII或UNICODE编码
            if codes <= 126:  # 若是半角字符
                new_string = new_string + chr(codes+65248) # 则转为全角
            else:
                new_string = new_string + i  # 若是全角,则不转换
        return new_string + space*(difference)  # 返回补齐空格后的字符串
    elif(way == "right"):
        difference = length - len(string)  # 计算限定长度时需要补齐多少个空格
        if difference == 0:  # 若差值为0则不需要补
            return string
        elif difference < 0:
            print('错误:限定的对齐长度小于字符串长度!')
            return None
        new_string = ''
        space = ' '
        for i in string:
            codes = ord(i)  # 将字符转为ASCII或UNICODE编码
            if codes <= 126:  # 若是半角字符
                new_string = new_string + chr(codes+65248) # 则转为全角
            else:
                new_string = new_string + i  # 若是全角,则不转换
        return  space*(difference) + new_string   # 返回补齐空格后的字符串
    elif(way == "center0"):
        difference = length - len(string)  # 计算限定长度时需要补齐多少个空格
        if difference == 0:  # 若差值为0则不需要补
            return string
        elif difference < 0:
            print('错误:限定的对齐长度小于字符串长度!')
            return None
        new_string = ''
        space = ' '
        for i in string:
            codes = ord(i)  # 将字符转为ASCII或UNICODE编码
            if codes <= 126:  # 若是半角字符
                new_string = new_string + chr(codes+65248) # 则转为全角
            else:
                new_string = new_string + i  # 若是全角,则不转换
        return  space*(int(difference/2)) + new_string + space*(int(difference/2))   # 返回补齐空格后的字符串
    elif(way == "center2"):
        difference = length - len(string)  # 计算限定长度时需要补齐多少个空格
        if difference == 0:  # 若差值为0则不需要补
            return string
        elif difference < 0:
            print('错误:限定的对齐长度小于字符串长度!')
            return None
        new_string = ''
        space = ' '
        for i in string:
            codes = ord(i)  # 将字符转为ASCII或UNICODE编码
            if codes <= 126:  # 若是半角字符
                new_string = new_string + chr(codes+65248) # 则转为全角
            else:
                new_string = new_string + i  # 若是全角,则不转换
        return  space*(int(difference/2+1)) + new_string + space*(int(difference/2))        
    elif(way == "center1"):
        difference = length - len(string)  # 计算限定长度时需要补齐多少个空格
        if difference == 0:  # 若差值为0则不需要补
            return string
        elif difference < 0:
            print('错误:限定的对齐长度小于字符串长度!')
            return None
        new_string = ''
        space = ' '
        for i in string:
            codes = ord(i)  # 将字符转为ASCII或UNICODE编码
            if codes <= 126:  # 若是半角字符
                new_string = new_string + chr(codes+65248) # 则转为全角
            else:
                new_string = new_string + i  # 若是全角,则不转换
        return  space*(int(difference/2)) + new_string + space*(int(difference/2 + 1))   # 返回补齐空格后的字符串
    elif(way == "center2"):
        difference = length - len(string)  # 计算限定长度需要补齐多少个空格
        if difference == 0:  # 若差值为0则不需要补
            return string
        elif difference < 0:
            print('错误:限定的对齐长度小于字符串长度!')
            return None
        new_string = ''
        space = ' '
        for i in string:
            codes = ord(i)  # 将字符转为ASCII或UNICODE编码
            if codes <= 126:  # 若是半角字符
                new_string = new_string + chr(codes+65248) # 则转为全角
            else:
                new_string = new_string + i  # 若是全角,则不转换
        return  space*(int(difference/2+1)) + new_string + space*(int(difference/2))   # 返回补齐空格后的字符串

那么使用了上面的函数后,运行的代码截图就应该为

代码如下:

'''
调用方法
#左边对齐
string1 = "字符串123"
result = duiqi(string1,10,"left")#对齐字符串,长度,对齐方式
print(result)
#右边对齐
string1 = "字符串123"
result = duiqi(string1,10,"right")#对齐字符串,长度,对齐方式
print(result)
#居中对齐这个有点麻烦,例如 “123” ,对齐长度 6 ,要补三个空格,前面一个后面两个还是前面两个后面一个,这个是可以设置的
对齐的方式有 center0 center1 center2
center1:前面的空格更少,后面的更多
center2:后面的空格更少,前面的更多
center0:两边空格一样多(需要字符合理——例: 字符串“123”, 长度为 “5”,不然要报错)
'''
def duiqi(string,length,way):
    if(way == "left"):
        difference = length - len(string)  
        if difference == 0:  # 若差值为0则不需要补
            return string
        elif difference < 0:
            print('错误:限定的对齐长度小于字符串长度!')
            return None
        new_string = ''
        space = ' '
        for i in string:
            codes = ord(i)  # 将字符转为ASCII或UNICODE编码
            if codes <= 126:  # 若是半角字符
                new_string = new_string + chr(codes+65248) # 则转为全角
            else:
                new_string = new_string + i  # 若是全角,则不转换
        return new_string + space*(difference)  # 返回补齐空格后的字符串
    elif(way == "right"):
        difference = length - len(string)  
        if difference == 0:  # 若差值为0则不需要补
            return string
        elif difference < 0:
            print('错误:限定的对齐长度小于字符串长度!')
            return None
        new_string = ''
        space = ' '
        for i in string:
            codes = ord(i)  # 将字符转为ASCII或UNICODE编码
            if codes <= 126:  # 若是半角字符
                new_string = new_string + chr(codes+65248) # 则转为全角
            else:
                new_string = new_string + i  # 若是全角,则不转换
        return  space*(difference) + new_string   # 返回补齐空格后的字符串
    elif(way == "center0"):
        difference = length - len(string)  
        if difference == 0:  # 若差值为0则不需要补
            return string
        elif difference < 0:
            print('错误:限定的对齐长度小于字符串长度!')
            return None
        new_string = ''
        space = ' '
        for i in string:
            codes = ord(i)  # 将字符转为ASCII或UNICODE编码
            if codes <= 126:  # 若是半角字符
                new_string = new_string + chr(codes+65248) # 则转为全角
            else:
                new_string = new_string + i  # 若是全角,则不转换
        return  space*(int(difference/2)) + new_string + space*(int(difference/2))   # 返回补齐空格后的字符串
    elif(way == "center2"):
        difference = length - len(string)  
        if difference == 0:  # 若差值为0则不需要补
            return string
        elif difference < 0:
            print('错误:限定的对齐长度小于字符串长度!')
            return None
        new_string = ''
        space = ' '
        for i in string:
            codes = ord(i)  # 将字符转为ASCII或UNICODE编码
            if codes <= 126:  # 若是半角字符
                new_string = new_string + chr(codes+65248) # 则转为全角
            else:
                new_string = new_string + i  # 若是全角,则不转换
        return  space*(int(difference/2+1)) + new_string + space*(int(difference/2))        
    elif(way == "center1"):
        difference = length - len(string)  
        if difference == 0:  # 若差值为0则不需要补
            return string
        elif difference < 0:
            print('错误:限定的对齐长度小于字符串长度!')
            return None
        new_string = ''
        space = ' '
        for i in string:
            codes = ord(i)  # 将字符转为ASCII或UNICODE编码
            if codes <= 126:  # 若是半角字符
                new_string = new_string + chr(codes+65248) # 则转为全角
            else:
                new_string = new_string + i  # 若是全角,则不转换
        return  space*(int(difference/2)) + new_string + space*(int(difference/2 + 1))   # 返回补齐空格后的字符串
    elif(way == "center2"):
        difference = length - len(string)  
        if difference == 0:  # 若差值为0则不需要补
            return string
        elif difference < 0:
            print('错误:限定的对齐长度小于字符串长度!')
            return None
        new_string = ''
        space = ' '
        for i in string:
            codes = ord(i)  # 将字符转为ASCII或UNICODE编码
            if codes <= 126:  # 若是半角字符
                new_string = new_string + chr(codes+65248) # 则转为全角
            else:
                new_string = new_string + i  # 若是全角,则不转换
        return  space*(int(difference/2+1)) + new_string + space*(int(difference/2))   # 返回补齐空格后的字符串        
    
 
 
 
print("\n\n")
print(duiqi("名字",10,"left"),end="")
 
print(duiqi("价格",10,"left"),end="")
 
print("")
 
print(duiqi("面条",10,"left"),end="")
 
print(duiqi("10RMB",10,"left"),end="")
 
print("")
print(duiqi("肉",10,"left"),end="")
 
print(duiqi("40RMB",10,"left"),end="")
print("\n\n")

### 如何将 Anaconda 的 Scripts 文件夹路径添加到系统的 PATH 环境变量中 #### 在 Windows 上配置 Anaconda Scripts 路径 在 Windows 中,可以通过以下方式将 Anaconda 的 `Scripts` 文件夹路径添加到系统的 PATH 环境变量: 1. **找到 Anaconda 安装目录** 假设 Anaconda 已经安装在默认位置 `C:\Users\YourUsername\Anaconda3`,那么其 `Scripts` 文件夹的路径应为 `C:\Users\YourUsername\Anaconda3\Scripts`。 2. **编辑系统环境变量** - 右键单击“此电脑”或“我的电脑”,选择“属性”。 - 进入“高级系统设置”,点击“环境变量”按钮。 - 在“系统变量”部分,找到并选中 `Path`,然后点击“编辑”。 3. **添加新路径** - 点击“新建”,输入 Anaconda 的 `Scripts` 文件夹路径,例如:`C:\Users\YourUsername\Anaconda3\Scripts`[^2]。 - 同样可以添加 Anaconda 主目录路径(如 `C:\Users\YourUsername\Anaconda3`),以便直接运行 Python 和 Conda 命令。 4. **保存更改** - 点击“确定”保存修改后的环境变量。 - 关闭所有打开的命令提示符窗口,重新打开一个新的命令提示符窗口以使更改生效。 #### 在 Linux 上配置 Anaconda Scripts 路径 对于 Linux 用户来说,通常通过 `.bashrc` 或 `.zshrc` 文件来永久性地更新 PATH 环境变量。以下是具体操作方法: 1. **打开终端** 使用任何文本编辑器(如 nano、vim)打开用户的 shell 配置文件。假设当前使用的 Shell 是 Bash,则执行以下命令: ```bash nano ~/.bashrc ``` 2. **追加路径** 在文件末尾添加如下两行内容,分别指向 Anaconda 的主目录和 `Scripts` 子目录: ```bash export PATH="/your/anaconda/path/bin:$PATH" export PATH="/your/anaconda/path/Scripts:$PATH" ``` 将 `/your/anaconda/path` 替换为实际的 Anaconda 安装路径,比如 `/home/user/anaconda3`[^1]。 3. **应用更改** 保存文件后,在同一终端中运行以下命令使其立即生效: ```bash source ~/.bashrc ``` #### 测试配置是否成功 无论是在 Windows 还是 Linux 下完成上述步骤之后,都可以测试配置是否正确无误。打开新的命令提示符或者终端窗口,尝试运行以下命令验证: ```bash conda --version python --version ``` 如果能够正常显示版本号信息,则说明配置已成功。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值