【Python-基础】字符串、原始字符串、长字符串

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("""
哈哈哈哈
哈哈哈哈
哈哈哈哈
哈哈哈哈
哈哈哈哈
哈哈哈哈
哈哈哈哈""")

哈哈哈哈
哈哈哈哈
哈哈哈哈
哈哈哈哈
哈哈哈哈
哈哈哈哈
哈哈哈哈
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值