📜  %s 在Python格式字符串中是什么意思?

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

%s 在Python格式字符串中是什么意思?

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

%s运算符放在要指定字符串的位置。要附加到字符串的值的数量应等于字符串值末尾的 %运算符后括号中指定的数量。

以下Python代码说明了执行字符串格式化的方式。

%s 的简单使用

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


Python3
# declaring a string variable
var1 = "Geek!"
var2 = "Geeks for Geeks"
  
# append multiple strings within a string
print("Hello %s Are you enjoying being at %s for preparations." % (var1, var2))


Python3
# declaring string variables
str1 = 'Understanding'
str2 = '%s'
str3 = 'at'
str4 = 'GeeksforGeeks'
  
# concatenating strings but %s not equal to string variables
final_str = "%s %s %s %s" % (str1, str3, str4)
  
# printing the final string
print("Concatenating multiple strings using Python '%s' operator:\n")
print(final_str)


Python3
# declaring string variables
str1 = 'Understanding'
str2 = '%s'
str3 = 'at'
str4 = 'GeeksforGeeks'
  
# concatenating strings
final_str = "%s %s %s %s" % (str1, str2, str3, str4)
  
# printing the final string
print("Concatenating multiple strings using Python '%s' operator:\n")
print(final_str)


Python3
# declaring string variables with dictionary
dct = {'str1': 'at',
       'str2': 'GeeksforGeeks',
       'str3': 'Understanding',
       'str4': '%s'}
  
# concatenating strings
final_str = "%(str3)s %(str4)s %(str1)s %(str2)s" % dct
  
# printing the final string
print("Concatenating multiple strings using Python '%s' operator:\n")
print(final_str)


Python3
# declaring string variables
str1 = 'Understanding'
str2 = 'integers'
str3 = 'at'
str4 = 'GeeksforGeeks = '
  
# declaring list variables
lst = [1, 2, 3]
  
# concatenating strings as well as list
final_str = "%s %s %s %s %s" % (str1, str2, str3, str4, lst)
  
# printing the final string
print("Concatenating multiple values using Python '%s' operator:\n")
print(final_str)


输出

Hey, Geek!

多个 %s

也可以使用 %s运算符将多个字符串附加到单个字符串中。字符串按照它们在括号中的位置顺序替换,其中有 %s 符号。使用以下代码片段说明了这一点:

蟒蛇3

# declaring a string variable
var1 = "Geek!"
var2 = "Geeks for Geeks"
  
# append multiple strings within a string
print("Hello %s Are you enjoying being at %s for preparations." % (var1, var2))

输出

将字符串映射到 %s

但是,此运算符的出现次数必须等于 % 符号后要替换的字符串数。否则,将抛出“TypeError: not enough arguments for format 字符串”类型的错误。

蟒蛇3

# declaring string variables
str1 = 'Understanding'
str2 = '%s'
str3 = 'at'
str4 = 'GeeksforGeeks'
  
# concatenating strings but %s not equal to string variables
final_str = "%s %s %s %s" % (str1, str3, str4)
  
# printing the final string
print("Concatenating multiple strings using Python '%s' operator:\n")
print(final_str)

错误

正确的代码

蟒蛇3

# declaring string variables
str1 = 'Understanding'
str2 = '%s'
str3 = 'at'
str4 = 'GeeksforGeeks'
  
# concatenating strings
final_str = "%s %s %s %s" % (str1, str2, str3, str4)
  
# printing the final string
print("Concatenating multiple strings using Python '%s' operator:\n")
print(final_str)

输出

Concatenating multiple strings using Python '%s' operator:

Understanding %s at GeeksforGeeks

使用字典订购 %s

使用输出中的字典键以任何附加顺序打印字符串。

蟒蛇3

# declaring string variables with dictionary
dct = {'str1': 'at',
       'str2': 'GeeksforGeeks',
       'str3': 'Understanding',
       'str4': '%s'}
  
# concatenating strings
final_str = "%(str3)s %(str4)s %(str1)s %(str2)s" % dct
  
# printing the final string
print("Concatenating multiple strings using Python '%s' operator:\n")
print(final_str)

输出

Concatenating multiple strings using Python '%s' operator:

Understanding %s at GeeksforGeeks

作为%s 的字符串列出

非字符串运算符也可以使用Python中的 %s 符号进行格式化。也可以使用此运算符插入和格式化元组。

蟒蛇3

# declaring string variables
str1 = 'Understanding'
str2 = 'integers'
str3 = 'at'
str4 = 'GeeksforGeeks = '
  
# declaring list variables
lst = [1, 2, 3]
  
# concatenating strings as well as list
final_str = "%s %s %s %s %s" % (str1, str2, str3, str4, lst)
  
# printing the final string
print("Concatenating multiple values using Python '%s' operator:\n")
print(final_str)

输出

Concatenating multiple values using Python '%s' operator:

Understanding integers at GeeksforGeeks =  [1, 2, 3]