📜  Python字符串中 %s 和 %d 之间的区别

📅  最后修改于: 2022-05-13 01:55:05.674000             🧑  作者: Mango

Python字符串中 %s 和 %d 之间的区别

在本文中,我们将看到Python中 %s 和 %d 之间的区别。在这里,我们从一次一个的正确解释开始,然后两个,最后比较这些。

%s运算符

% 符号在Python中使用,具有多种数据类型和配置。它用作参数说明符和字符串格式化程序。 %s 专门用于将字符串连接在一起。它允许我们格式化字符串中的值。它用于将另一个字符串合并到一个字符串中。它自动提供从 value 到字符串的类型转换。

%s运算符放在要指定字符串的位置。要附加到字符串的值的数量应等于字符串值末尾的 %运算符后括号中指定的数量。以下代码说明了 %s 符号的用法:

Python3
# declaring a string variable
name = "Geek"
#append a string within a string 
print("Hey, %s!" % name)


Python3
#declaring numeric variables
num = 2021
#concatenating numeric value within string
print("%d is here!!" % num)


Python3
#declaring rational number
frac_num = 8/3
#concatenating numeric value within string
print ("Rational number formatting using %d")
print("%d is equal to 8/3 using this operator." % frac_num)
  
#declaring decimal number
dec_num = 10.9785
#concatenating numeric value within string
print ("Decimal number formatting using %d")
print("%d is equal to 10.9785 using this operator." % dec_num)


Python3
name = "Sita"
age = 22
print("Using %s and %d both")
print ("%s's age is %d."%(name,age))


Python3
name = "Sita"
age = 22
print("Using %s ")
print ("%s's age is %s."%(name,age))


Python3
name = "Sita"
age = 22
print("Using %d")
print ("%d's age is %d."%(name,age))


输出

Hey, Geek!

%d运算符

%d运算符用作占位符来指定整数值、小数或数字。它允许我们在字符串或其他值中打印数字。 %d运算符放在要指定整数的位置。浮点数会自动转换为十进制值。

蟒蛇3

#declaring numeric variables
num = 2021
#concatenating numeric value within string
print("%d is here!!" % num)

输出

2021 is here!!

小数和有理数四舍五入到绝对整数部分,小数点后的数被刮掉,即只提取整数。以下代码说明了 %d 与小数和小数的用法:

蟒蛇3

#declaring rational number
frac_num = 8/3
#concatenating numeric value within string
print ("Rational number formatting using %d")
print("%d is equal to 8/3 using this operator." % frac_num)
  
#declaring decimal number
dec_num = 10.9785
#concatenating numeric value within string
print ("Decimal number formatting using %d")
print("%d is equal to 10.9785 using this operator." % dec_num)

输出

Rational number formatting using %d
2 is equal to 8/3 using this operator.
Decimal number formatting using %d
10 is equal to 10.9785 using this operator.

%s 和 %d运算符

我们也可以在单个程序中使用运算符的组合。在此之前,先通过下面给出的比较来清除一些概念:

%s%d
It is used as a placeholder for string values. It is used as a placeholder for numeric values.
Uses string conversion via str() before formatting.Uses decimal conversion via int() before formatting.
%s can accept numeric values also and it automatically does the type conversion.  In case a string is specified for %d operator a type error is returned.

蟒蛇3

name = "Sita"
age = 22
print("Using %s and %d both")
print ("%s's age is %d."%(name,age))

输出

Using %s and %d both
Sita's age is 22.

注 1: %s 自动将数值转换为字符串而不会抛出错误。

蟒蛇3

name = "Sita"
age = 22
print("Using %s ")
print ("%s's age is %s."%(name,age))

输出

Using %s 
Sita's age is 22.

注 2:然而,%d 只能用于数值。否则,返回错误。

蟒蛇3

name = "Sita"
age = 22
print("Using %d")
print ("%d's age is %d."%(name,age))

错误

Using %d
Traceback (most recent call last):
 File "", line 4, in 
TypeError: %d format: a number is required, not str