简体   繁体   English

写字符串时python中的%是什么意思

[英]what does % mean in python when writing a string

So I was looking at a python tutorial, and in a statement, it used %.所以我在看 python 教程,在一份声明中,它使用了 %。

print ("Total Employee %d" % Employee.empCount)

Can someone please take their time and describe to me what that means.有人可以花点时间向我描述一下这意味着什么。 I'm pretty sure that it doesn't signify a division.我很确定这并不表示分裂。

Python uses C-style string formatting to create new, formatted strings. Python 使用 C 风格的字符串格式化来创建新的格式化字符串。 The "%" operator is used to format a set of variables enclosed in a "tuple" (a fixed size list), together with a format string, which contains normal text together with "argument specifiers", special symbols like "%s" and "%d". “%”运算符用于格式化包含在“元组”(固定大小列表)中的一组变量,以及格式字符串,格式字符串包含普通文本和“参数说明符”,特殊符号如“%s”和“%d”。

在此处输入图像描述

the % sign in this case is supposed to be used for string formatting.本例中的 % 符号应该用于字符串格式化。 This is an old technique and can be used with and f-string now.这是一项古老的技术,现在可以与 f-string 一起使用。 % is mostly used in java and not python. % 主要用于 java 而不是 python。

Python Program for Old Style Formatting of Integers also used % Python 旧式整数格式化程序也使用了 %

Integer1 = 12.3456789
print("Formatting in 3.2f format: ") 
print('The value of Integer1 is %3.2f' %Integer1) 
print("\nFormatting in 3.4f format: ") 
print('The value of Integer1 is %3.4f' %Integer1) 

Output: Output:

Formatting in 3.2f format: 
The value of Integer1 is 12.35

Formatting in 3.4f format: 
The value of Integer1 is 12.3457

also use as也用作

print("writing integer in a string: %s" %Integer1) 

output output

writing integer in a string: 12.3456789

% Employee.empCount is a variable and %d for print integer variable.That's mean value of variable % Employee.empCount print in place of %d . % Employee.empCount是一个变量, %d用于打印 integer 变量。这是变量% Employee.empCount打印的平均值代替%d

% sign use for give reference of variable. % 符号用于给出变量的引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM