python學習:字串操作

十五樓亮哥發表於2015-01-27

1:雙引號轉義

<span style="font-size:18px;"><strong>print "he\"llo"</strong></span>
只需要在雙引號前面加一個反斜槓。

輸出結果:he"llo


2:字串的拼接

(1)可以用+號

<span style="font-size:18px;"><strong>print "hello" +" "+ "wolrd"</strong></span>

(2)可以用逗號

<span style="font-size:18px;"><strong>print "hello","world"</strong></span>

(3)可以用*數字

<span style="font-size:18px;"><strong>print "hello"*3</strong></span>

輸出結果:

hello wolrd
hello world
hellohellohello


3:字串長度

<span style="font-size:18px;"><strong>print len("hello")</strong></span>

使用len()函式。輸出結果5


4:型別轉換:字串轉int

<span style="font-size:18px;"><strong>print int("111") + 1</strong></span>
輸出結果 112


5:型別轉換:int轉字串

<span style="font-size:18px;"><strong>print "aaa" + str(1)</strong></span>

使用函式str

輸出結果:aaa1




相關文章