Python String类型在实际应用中可以给我们带来非常大的好处。在接下来的文章中,我们将会在这里为大家详细介绍一下有关Python String类型的相关应用方式,希望大家可以充分的掌握这一应用。
Python String类型应用代码示例:
>>> '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)
注意:
单引号''和双引号""作用相同,都用来表示字符串,但是单引号''中可以有双引号"",双引号""中也可以有单引号'',但是如果双引号""中使用双引号""或是单引号''中使用单引号''时,必须使用转义字符\,例如\'或\"。
行末尾\表示字符串换行。
字符串前的r表示纯字符串,此时字符串中的转义字符失效。
+表示字符串的链接。
[]可以用来索引字符串中的字符,但是不能用来修改字符串中的字符。