String formatting in python

The string formatting method in python is as follows:

                      Way

                        Remark

print(content1,content2,content3,...) Use the "," symbol for content splicing.
print(String 1+String 2+String 3...) If it is not a string, string conversion [ str() ] is required , otherwise an error will be reported.
print("...%s...%d...%f..."%(Variable 1, Variable 2, Variable 3))

%s represents a placeholder for a string;

%d is the placeholder for integer (int), where d is the abbreviation of digit (number);

%f is a placeholder for a floating point number (float), which retains 6 digits after the decimal point by default.

The value will be rounded off.

print(f"...{variable name}...") f is the abbreviation of format (setting format)

The corresponding code is as follows:

name = '笨小孩'
sex = '男'
age = 24
height = 168.5

# 方式一:
print(name, sex, age, height)

# 方式二:  print("我叫" + name + "性别为" + sex + "今年" + age+"身高是"+height)
# 报错:TypeError: can only concatenate str (not "int") to str
print("我叫" + name + "性别为" + str(sex) + "今年" + str(age) + "身高是" + str(height))

# 方式三:
print("我叫%s,性别为%s,今年%d,身高是%f" % (name, sex, age, height))

# 方式四
print(f"我叫{name},性别为{sex},今年{age},身高是{height}")

operation result:

Extended analysis:

In real life, it is possible to control the accuracy of the numbers in the output string, but how to do it?

We can use the auxiliary symbol "mn" to control the width and precision of the data. Where

  • m, controls the width. The set width is greater than the number itself. By default, spaces will be used to complete the width, and 0 can also be used to complete the width.
                If the set width is smaller than the number itself, it will not take effect.
    n, controls the decimal point precision and will round the decimal value.
  print("...%s...%d...%f..."%(variable name 1, variable name 2, variable name 3) print(...{variable name}...)
    integer

       %md,    

  %0md

      {Variable name: md},

      {Variable name: 0md}

    floating point number

%.nf,

m.nf,

%m.nf

      {Variable name:.nf},

      {Variable name: m.nf},

      {Variable name:0m.nf}

Case 1: Width control

name = '笨小孩'
sex = '男'
age = 24
height = 168.5

"""age宽度设置为5>2,默认会用空格补足宽度,也可以使用0进行补全!!!"""

# 方式1
print("我叫%s,性别为%s,今年%5d,身高是%f" % (name, sex, age, height))
print(f"我叫{name},性别为{sex},今年{age:5d},身高是{height}")

# 方式2
print("我叫%s,性别为%s,今年%05d,身高是%f" % (name, sex, age, height))
print(f"我叫{name},性别为{sex},今年{age:05d},身高是{height}")

"""age宽度设置为1>2,不生效!!!"""

# 方式1
print("我叫%s,性别为%s,今年%1d,身高是%f" % (name, sex, age, height))
print(f"我叫{name},性别为{sex},今年{age:1d},身高是{height}")
# 方式2

print("我叫%s,性别为%s,今年%01d,身高是%f" % (name, sex, age, height))
print(f"我叫{name},性别为{sex},今年{age:1d},身高是{height}")

The result is as follows: 

Situation 2: Precision control

name = '笨小孩'
sex = '男'
age = 24
height = 168.5

"""身高取值范围保留2位小数"""

# 方式1
print("我叫%s,性别为%s,今年%d,身高是%.2f" % (name, sex, age, height))
print(f"我叫{name},性别为{sex},今年{age},身高是{height:.2f}")

# 方式2
print("我叫%s,性别为%s,今年%d,身高是%8.2f" % (name, sex, age, height))
print(f"我叫{name},性别为{sex},今年{age},身高是{height:8.2f}")

# 方式3
print("我叫%s,性别为%s,今年%d,身高是%4.2f" % (name, sex, age, height))
print(f"我叫{name},性别为{sex},今年{age},身高是{height:4.2f}")

# 方式4
print("我叫%s,性别为%s,今年%d,身高是%08.2f" % (name, sex, age, height))
print(f"我叫{name},性别为{sex},今年{age},身高是{height:08.2f}")

 The result is as follows: 

Guess you like

Origin blog.csdn.net/tjfsuxyy/article/details/130211753