Python3 String.md
' '
" "
''' '''
""" """
\
r
+
*
' '
" "
:表示字符串。在行尾添加 '' 表示换行接着写,不是字符串换行("\n")。
''' '''
""" """
: 表示跨多行的字符串。行尾换行符会自动添加到字符串中,如果不想换行可以在行尾加上\
。
# ''、"" 直接换行,会出现错误
>>> a = 'hello
File "<stdin>", line 1
a = 'hello
^
SyntaxError: EOL while scanning string literal
# 用换行符 '\' 来换行写。注意,'line1' 与 '\' 之间的空格也是字符串中的一部分
>>> a = 'line1 \
... line2 \
... line3 \
... ';
>>> a
'line1 line2 line3 '
>>> print(a);
'line1 line2 line3 '
# ''' '''、""" """ 会自动换行,在行尾自动添加 "\n"
>>> a = """line1
... line2
... line3
... """;
>>>a
'line1\nline2\nline3\n'
>>> print(a);
line1
line2
line3
# 如果想要 ''' ''' 、""" """ 不执行换行,可以在行尾添加 '\' 来取消换行
>>> a = 'aaa \
... bbb\
... '
>>> print(a);
aaa bbb
>>> a = '''line1\
... line2\
... line3\
... ''';
>>> a
'line1line2line3'
>>> print(a);
line1line2line3
\
:\
除了上面的用法,还可以用来 escape(转义) character。
>>> 'doesn\'t' # use \' to escape the single quote...
"doesn't"
>>> "doesn't" # ...or use double quotes instead
"doesn't"
>>> '"Yes," he said.'
'"Yes," he said.'
>>> "\"Yes,\" he said."
'"Yes," he said.'
>>> '"Isn\'t," she said.'
'"Isn\'t," she said.'
r
: 在字符串前面加上一个 r
,表示使用原始字符串。即转义字符会无效,字符串会原样输出。
>>> print('C:\some\name') # here \n means newline!
C:\some
ame
>>> print(r'C:\some\name') # note the r before the quote
C:\some\name
+
*
:字符串可以用 +
来连接,用 *
来重复多次。
>>> a = 'he' + 2*'l' + 'o';
>>> a
'hello'
>>> print(a);
hello
相邻的两个或多个 string literals (用引号引起来的) 会自动连接。
>>> a = 'hello' ' ' 'world' '!'
>>> a
'hello world!'
>>> print(a);
hello world!
然而,这种方式只能用于两个字符串的连接,变量或者表达式是不行的,只能用 +
来实现。
>>> a = 'hello ';
>>> b = a 'world';
File "<stdin>", line 1
b = a 'world';
^
SyntaxError: invalid syntax
>>> a = 'he' 2*'l' 'o';
File "<stdin>", line 1
a = 'he' 2*'l' 'o';
^
SyntaxError: invalid syntax
Strings can be indexed (subscripted), with the first character having index 0. There is no separate character type; a character is simply a string of size one. Indices may also be negative numbers, to start counting from the right.
字符串可以 索引(index)
,第一个字符的索引值为 0 。Python 没有单独的字符类型;字符就是长度为 1 的字符串。索引
也可以是负值,此时从右侧开始计算。
>>> a = 'hello' ' ' 'world' '!'
>>> a
'hello world!'
>>> a[0]
'h'
>>> a[5]
' '
>>> a[-1]
'!'
注意:因为,0 和 -0 是一样的,所以负的索引从 -1 开始。
In addition to indexing, slicing
is also supported. While indexing is used to obtain individual characters, slicing allows you to obtain substring.
除了索引,字符串还支持 切片
。索引用于获得单个字符,切片
让你获得字符串。
slice 正向是从 0 开始,反向负的从 -1 开始。
[X: Y] slice 第一个 index 表示起始位置(included),第二个 index 表示结束的位置(excluded)。
>>> a = 'hello' ' ' 'world' '!'
>>> a
'hello world!'
>>> a[0: 5] // 左边,从下标 0 起,到下标为 5 的元素,不包括 a[5]。
'hello'
>>> a[-6: -1] // 记住,不包括 a[-1]
'world'
切片
的索引
有非常有用的默认值:省略第一个索引,则默认为 0,省略第二个索引,则默认为字符串的大小。
因此,a = a[ : i] + a[i: ]
>>> a = 'hello' ' ' 'world' '!';
>>> a
'hello world!'
>>> a[ : 5] # 不包括 a[5]
'hello'
>>> a[5: ] # 包括 a[5]
' world!'
Python 字符串是不可以改变的。因此,赋值给字符串索引的位置会导致错误。
>>> a = 'hello' ' ' 'world' '!'
>>> a
'hello world!'
>>> a[0: 5] = a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
如果你需要一个不同的字符串,你应该创建一个新的。
>>> b = 'hi' + a[5: ];
>>> b
'hi world!'