python 中的%s是什么意思呢?,, 今天忽然想写Pyt


今天忽然想写Python中的%s的意思,它怎么理解呢,我查阅了一下相关文献,然后结合了自己的理解,分析如下:

这是一个字符串格式化语法(这是从c 中调用的)

具体请参阅 http://www.diveintopython.net/native_data_types/formatting_strings.html

Python支持将值格式化为字符串,虽然这可以包括非常复杂的表达式,但是最基本的用法是将值插入到%s 占位符的字符串中。

title = "hello  world!"print( "%s" %title)
结果:hello world!

注意:这里的 %s 被替换为后面 % 符号后窑传递给字符串的内容(他可以包括单个和多个字符串)

上面是单个内容,下面举例说一下多个内容,此处用元组来说明插入多个字符串

apple_price=5apple_weight =9shopping_apple_money = apple_weight*apple_priceprint("苹果的单价是%s    苹果的重量是%s    所以总共花费了 %s  "   % (apple_price,apple_weight,shopping_apple_money))结果为:      苹果的单价是5    苹果的重量是9    所以总共花费了 45

python 中的%s是什么意思呢?

评论关闭