單引號(')
或雙引號(")
都可以,官方文件提到差別只有用其中一個時不需對另一個標示為逃脫字元
The only difference between the two is that within single quotes you don’t need to escape " (but you have to escape ') and vice versa.
可以使用反斜號(\)
逃脫某些字元。官方逃脫字元列表在這裡
使用三個單引號(''')
或是三個雙引號(""")
。可以使用反斜號(\)
達到不換行的效果。
針對單行宣告,PEP8並無特別推薦使用單引號或雙引號。對於多行,PEP8建議使用三個雙引號(""")
This PEP does not make a recommendation for this.
For triple-quoted strings, always use double quote characters to be consistent with the docstring convention in PEP 257.
https://www.python.org/dev/peps/pep-0008/#string-quotes
s1 = "single line's \"declaration\""
s2 = 'another single line\'s "declaration"'
print(s1)
print(s2)
m1 = """\
first line
second line \
,follow by second line
"""
print(m1)
immutable。宣告後就沒辦法修改。
# immutable
wrong_str = "irenman"
print(wrong_str)
print("id of wrong_str: {}".format( id(wrong_str) ))
print("error char is index 2 \"{}\"".format(wrong_str[2]))
wrong_str_2 = "irenman"
print(wrong_str_2)
print("id of wrong_str_2: {}".format( id(wrong_str_2) ))
# edit
wrong_str[2] = "o"