1、字符串
字符串可以使用单引号也可以使用双引号,使用 + 号就是字符串拼接。
"5"+"8"
'58'
'5'+'8'
'58'
'5'+"8"
'58'
'58'+'58'
'5858'
转义字符 ’ \ ',转义字符与其他语言是一样的。
string = "C:\now"
print(string)
C:
ow
string = "C:\\now"
print(string)
C:\now
'Let's go'
SyntaxError: unterminated string literal (detected at line 1)
'Let\'s go'
"Let's go"
2、原始字符串
为了避免转义字符太多使代码看起来混乱,Python引入了原始字符串,只需要在字符串前加一个 r /R即可,使用如下:
string = r"C:\now"
print(string)
C:\now
string = r'C:\now'
print(string)
C:\now
3、长字符串
当一个打印首诗时,需要用到很多 \n,但是也可以使用三个引号,输入格式与输出格式一样。
print("哈哈哈哈\n哈哈哈哈\n哈哈哈哈\n哈哈哈哈\n哈哈哈哈\n哈哈哈哈\n哈哈哈哈\n")
哈哈哈哈
哈哈哈哈
哈哈哈哈
哈哈哈哈
哈哈哈哈
哈哈哈哈
哈哈哈哈
print("""
哈哈哈哈
哈哈哈哈
哈哈哈哈
哈哈哈哈
哈哈哈哈
哈哈哈哈
哈哈哈哈""")
哈哈哈哈
哈哈哈哈
哈哈哈哈
哈哈哈哈
哈哈哈哈
哈哈哈哈
哈哈哈哈