赞
踩
字符串可以使用单引号也可以使用双引号,使用 + 号就是字符串拼接。
"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"
为了避免转义字符太多使代码看起来混乱,Python引入了原始字符串,只需要在字符串前加一个 r /R即可,使用如下:
string = r"C:\now"
print(string)
C:\now
string = r'C:\now'
print(string)
C:\now
当一个打印首诗时,需要用到很多 \n,但是也可以使用三个引号,输入格式与输出格式一样。
print("哈哈哈哈\n哈哈哈哈\n哈哈哈哈\n哈哈哈哈\n哈哈哈哈\n哈哈哈哈\n哈哈哈哈\n") 哈哈哈哈 哈哈哈哈 哈哈哈哈 哈哈哈哈 哈哈哈哈 哈哈哈哈 哈哈哈哈 print(""" 哈哈哈哈 哈哈哈哈 哈哈哈哈 哈哈哈哈 哈哈哈哈 哈哈哈哈 哈哈哈哈""") 哈哈哈哈 哈哈哈哈 哈哈哈哈 哈哈哈哈 哈哈哈哈 哈哈哈哈 哈哈哈哈
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。