Python String類型基本應(yīng)用情況分析
Python String類型在實(shí)際應(yīng)用中可以給我們帶來非常大的好處。在接下來的文章中,我們將會在這里為大家詳細(xì)介紹一下有關(guān)Python String類型的相關(guān)應(yīng)用方式,希望大家可以充分的掌握這一應(yīng)用。
Python String類型應(yīng)用代碼示例:
- >>> 'hello world'
- 'hello world'
- >>> "hello world"
- 'hello world'
- >>> "doesn't"
- "doesn't"
- >>> 'hello "tom"'
- 'hello "tom"'
- >>> "hello,\"tom\""
- 'hello,"tom"'
- >>> hello="hello,\
- i miss you."
- >>> print(hello)
- hello,i miss you.
- >>> print(r"hello\n world")
- hello\n world
- >>> word='hello'+'A'
- >>> print(word)
- helloA
- >>> word[0:5]+'B'
- 'helloB'
- >>> word[-1]
- 'A'
- >>> len(word)
注意:#t#
單引號''和雙引號""作用相同,都用來表示字符串,但是單引號''中可以有雙引號"",雙引號""中也可以有單引號'',但是如果雙引號""中使用雙引號""或是單引號''中使用單引號''時(shí),必須使用轉(zhuǎn)義字符\,例如\'或\"。
行末尾\表示字符串換行。
字符串前的r表示純字符串,此時(shí)字符串中的轉(zhuǎn)義字符失效。
+表示字符串的鏈接。
[]可以用來索引字符串中的字符,但是不能用來修改字符串中的字符。
len()用來獲得字符串的長度。
以上就是對Python String類型的相關(guān)介紹。