python中的%s%是什麼意思

來源:互聯網
上載者:User

標籤:python   資料類型   

它是一個字串格式化文法(它從C借用。


Python支援將值格式化為字串。雖然這可以包括非常複雜的運算式,但最基本的用法是將值插入到%s 預留位置的字串中。


樣本1:

#!/usr/bin/env python# -*- coding: utf-8 -*-name = "Tom"print "Hello %s" % name

結果:

Hello Tom


樣本2:

#!/usr/bin/env python# -*- coding: utf-8 -*-name = raw_input("who are you?")print "hello %s" % (name,)

結果:

who are you?dengaohello dengao

註:該  %s 令牌允許我插入(和潛在的格式)的字串。請注意,  %s 令牌被替換為% 符號後傳遞給字串的任何內容。還要注意,我也在這裡使用一個元組(當你只有一個使用元組的字串是可選的)來說明可以在一個語句中插入和格式化多個字串。


只是為了協助您更多,以下是您如何在一個字串中使用多種格式

"Hello %s, my name is %s" % ('john', 'mike') # Hello john, my name is mike".

如果您使用int而不是字串,請使用%d而不是%s。

"My name is %s and i'm %d" % ('john', 12) #My name is john and i'm 12.


python中的%s%是什麼意思

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.