Python 學習筆記(七)Python字串(四),學習筆記python

來源:互聯網
上載者:User

Python 學習筆記(七)Python字串(四),學習筆記python

輸入輸出

 輸入函數 raw_input (Python3:input)

 1 >>> raw_input("請輸入一個字母") #擷取輸入內容的一個函數 2 請輸入一個字母A 3 'A' 4 >>> 5 >>> name = raw_input("Please input your name:") #輸入得內容賦值給一個變數 6 Please input your name:tom 7 >>> name 8 'tom' 9 >>> x =raw_input("how old are you?")10 how old are you?x =raw_input("how old are you?")11 >>> x =raw_input("how old are you?")12 how old are you?2213 >>> x14 '22'15 >>> type(x)  #通過raw_input 得到的結果是一個字串類型16 <type 'str'>17 >>> help(raw_input) #查看raw_input詳細內容18 Help on built-in function raw_input in module __builtin__:19 20 raw_input(...)21     raw_input([prompt]) -> string   傳回值字串22 23     Read a string from standard input.  The trailing newline is stripped.24     If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.25     On Unix, GNU readline is used if enabled.  The prompt string, if given,26     is printed without a trailing newline before reading.

 

print()格式化

print 在python2中是一個語句 ,在python3中print() 是一個函數

格式化   指定一種格式,按照某種格式列印出來,

    制定一種模板,在模板中某個地方留出一些空位,在空位上填上某些內容,那麼這些空位就需要一些符號來表示,通常稱之為預留位置

%s  字串預留位置

%d  整數預留位置

%f  浮點數預留位置

 3 >>> "I LIKE %s" 4 'I LIKE %s' 5 >>> "I Like %s"%"Python"  #用%之後的Python 來填充%s這個預留位置 6 'I Like Python' 7 >>> a = "I Like %s"12 >>> a %"java"13 'I Like java'14 >>> print "%s was born in %d "%("tom",2018)  #括弧中為字串和數字15 tom was born in 201816 >>>

用花括弧{}索引值 預留位置 Python 推薦使用  {} 

   str.format() 格式化

1 >>> s = "I like {0}"2 >>> s.format("Python")3 'I like Python'4 >>> s2 ="%s was born in %d "%("tom",2018)5 >>> s2.format("tom",2018)6 'tom was born in 2018 '

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.