python学习-字符串

字符串

1. 字符串

由有限的字符组成的有限序列。表述如下:str=“helle,world” str1=‘hello,world’ 由单引号或双引号围起来的单个或多个字符表示一个字符串。

2. 转义字符

反斜杠\ +字符 组合表示其他含义

  • \n 表示换行,
  • \t表示制表符
  • 要在字符串中表示单引号或双引号 ’ 或" .
  • 同样 要在字符串中表示 "\ “就要用”\"来表示。
  • 如果不希望字符串中的\表示转义,我们可以通过在字符串的最前面加上字母r来加以说明:
str="you ask:\"where are you from?\""
str1 = r'\"hello, world!\"'
str2 = r'\t\\hello, world!\\\t'
print(str)  #`输出显示 you ask:"where are you from?" 
print(str1, str2) #\"hello, world!\"    \t\\hello, world!\\\t

3. 字符串运算

  • 用+号来表示两个字符串拼接
    str=‘hello’
    str1=‘world’
    str2=str+’ '+str1
    print(str2) #输出结果:hello world
  • 判断一个字符串是否包含另一个字符串包含 ‘in’或者‘not in’
str="hello boy hello girl hello dear"
str1="hello bor" 
str3=hello boy" 
print(str1 in str)
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值