1. 程式人生 > >python 中的%s是什麽意思呢?

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

表達式 .html 格式 字符串 http logs ice 復雜 format

今天忽然想寫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=5

apple_weight =9

shopping_apple_money = apple_weight*apple_price

print("蘋果的單價是%s    蘋果的重量是%s    所以總共花費了 %s  "   % (apple_price,apple_weight,shopping_apple_money))

結果為:
      蘋果的單價是5    蘋果的重量是9    所以總共花費了 
45

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