python 格式化输出方法大全

在python种格式化输出有三种方法:
NB(注意): # 后面的部分表示输出结果。
第一种是类似于c语言的格式化输出,采用%s,%d等等的形式来进行输出,代码如下:

class Debug:
    def __init__(self):
        self.str1 = "hello"
        self.num = 5
        
        
    def formatPrint(self):
        print("this is a string: %s" % self.str1)    # this is a string: hello
        print("the number is %d" % self.num)         # the number is 5


main = Debug()
main.formatPrint()

进一步我们讨论当格式化输出多个变量的情况:

class Debug:
    def __init__(self):
        self.str1 = "hello"
        self.str2 = "world"
        self.str3 = "!"
        self.num1 = 5
        self.num2 = 1.0
        self.num3 = 2.2
        
        
    def formatPrint(self):
        print("three strings are: %s, %s, %s" % (self.str1, self.str2, self.str3))    # three strings are: hello, world, !
        print("three numbers are: %d, %f, %.3f" % (self.num1, self.num2, self.num3))  # three numbers are: 5, 1.000000, 2.200


main = Debug()
main.formatPrint()

我们可以看到当多个变量格式化输出时,我们在对应位置使用%s,%d, %f等等后使用()将我们所需要输出的变量整体括起来并跟在符号%后来完成输出。%s对应于字符串格式输出,%d对应于整数格式输出,而%f我们看到1.0的输出结果为1.000000,小数点后保留6位有效数字输出,%.3f对应于小数点后保留3位有效数字输出,由2.2的输出结果2.200得出。

第二种是使用format进行格式化输出,代码如下:

class Debug:
    def __init__(self):
        self.str1 = "hello"
        self.num = 5
        
        
    def formatPrint(self):
        print("this is a string: {}".format(self.str1))    # this is a string: hello
        print("the number is {}".format(self.num))         # the number is 5


main = Debug()
main.formatPrint()

同上,进一步我们讨论当格式化输出多个变量的情况:

class Debug:
    def __init__(self):
        self.str1 = "hello"
        self.str2 = "world"
        self.str3 = "!"
        self.num1 = 5
        self.num2 = 1.0
        self.num3 = 2.2
        
        
    def formatPrint(self):
        print("three strings are: {}, {}, {}".format(self.str1, self.str2, self.str3))    # three strings are: hello, world, !
        print("three numbers are: {}, {}, {}".format(self.num1, self.num2, self.num3))    # three numbers are: 5, 1.0, 2.2


main = Debug()
main.formatPrint()

我们可以看到,当使用format进行格式化输出时,只需要在想要输出的位置写下花括号{},并在使用.format后的()中按照对应顺序写下对应的变量名称即可。那有没有可能在花括号中更改顺序呢,是可以的,代码如下:

class Debug:
    def __init__(self):
        self.str1 = "hello"
        self.str2 = "world"
        self.str3 = "!"
        self.num1 = 5
        self.num2 = 1.0
        self.num3 = 2.2
        
        
    def formatPrint(self):
        print("three strings are: {1}, {2}, {0}".format(self.str1, self.str2, self.str3))    # three strings are: world, !, hello
        print("three numbers are: {0}, {2}, {1}".format(self.num1, self.num2, self.num3))    # three numbers are: 5, 2.2, 1.0


main = Debug()
main.formatPrint()

我们可以看到输出结果为 three strings are: world, !, hellothree numbers are: 5, 2.2, 1.0,变量输出的顺序已经发生了改变,因为我们赋予了花括号值,这个值就对应于.format()小括号中的对应位置的变量。相比于第一种格式化输出方法,这一种已经便捷了许多。

第三种为f string格式化输出,代码如下:

class Debug:
    def __init__(self):
        self.str1 = "hello"
        self.num = 5
        
        
    def formatPrint(self):
        print(f"this is a string: {self.str1}")    # this is a string: hello
        print(f"the number is: {self.num}")         # the number is 5


main = Debug()
main.formatPrint()

推荐使用f string方式进行输出,相对比较方便。但是唯一不足之处是当使用Anaconda Spyder编译器时,f string种的所有内容包括花括号{}中的内容都是以字符串的颜色(绿色)显示,不是非常容易辨认,但是当使用Pycharm编译器时,{}中的内容会以正常的变量颜色(黑色)进行显示,很容易区分。
同上,进一步我们讨论当格式化输出多个变量的情况:

class Debug:
    def __init__(self):
        self.str1 = "hello"
        self.str2 = "world"
        self.str3 = "!"
        self.num1 = 5
        self.num2 = 1.0
        self.num3 = 2.2
        
        
    def formatPrint(self):
        print(f"three strings are: {self.str1}, {self.str2}, {self.str3}")    # three strings are: hello, world, !
        print(f"three numbers are: {self.num1}, {self.num2}, {self.num3}")    # three numbers are: 5, 1.0, 2.2


main = Debug()
main.formatPrint()

我们可以看到,当使用f string格式化输出时,只需要在想要输出的位置写上变量名称,并以花括号{}括起来即可,非常方便。变量输出的顺序可以任意调整。

猜你喜欢

转载自blog.csdn.net/u011699626/article/details/108420303