python基础: 字符串操作

字符串

  • 定义: 使用引号括起来的一串字符
  • 一对单引号、一对双引号、三对单引号、三对双引号
  • 转义:使用 ‘\’,使原来有特殊含义的字符变成普通字符,也可以在定义字符串的前面加一个’r’
    • 字符串拼接

      s1 = 'hello'
      s2 = 'world'
      # 可以使用'+'将字符串拼接在一起
      s3 = s1 + s2
      print(s3)
      
      # 输出结果:
      	helloworld
      
    • 字符串重复

      # '*'可以重复前面的字符串若干次
      s4 = 'abc' * 3
      print(s4)
      # 输出结果:
      	abcabcabc
      
    • 统计字符串长度

      s1 = 'hello'
      # len函数统计字符串长度
      print(len(s1))
      # 输出结果:
      	5
      
    • 字符串提取

      s = 'abcdefg'
      # 从开头进行提取,下标从0开始
      print(s[0])
      # 从末尾进行提取,下标从-1开始
      print(s[-1])
      # 输出结果:
      	a
      	g
      
    • 字符串切片

      s = 'abcdefg'
      # 格式:s[开始:结束:步进]
      print(s[1:3])
      # 当一边的边界省略,则提取到改侧的边缘
      print(s[1:])
      print(s[:4])
      print(s[1:-2])
      print(s[:-3])
      print(s[-3:])
      # 指定步进值,默认为1
      print(s[::2])
      # 逆序提取
      print(s[::-1])
      
      # 输出结果:
      	bc
      	bcdefg
      	abcd
      	bcde
      	abcd
      	efg
      	aceg
      	gfedcba
      
    • 字符串格式化操作

      # %s:字符串
      # %d:整型
      # %f:浮点
      # %c:字符(一个字符)
      name = '二狗'
      like = '大花'
      age = 18
      print('俺叫%s,暗恋%s,她今年%d岁' % (name, like, age))
      
      # python中特有的解决方案
      print('俺叫{},暗恋{},她今年{}岁'.format(name, like, age))
      print('俺叫{2},暗恋{1},她今年{0}岁'.format(name, like, age))
      print('俺叫{l},暗恋{n},她今年{a}岁'.format(n=name, l=like, a=age))
      
      # 输出结果:
      	俺叫二狗,暗恋大花,她今年18岁
      	俺叫二狗,暗恋大花,她今年18岁
      	俺叫18,暗恋大花,她今年二狗岁
      	俺叫大花,暗恋二狗,她今年18岁
      
    • 字符串切割与拼接

      s = 'I love you more than i can say'
      # 切割字符串
      # sep:指定按照什么进行切割,默认按照空格切割
      # maxsplit:指定最大切割次数,默认不限制次数
      # ret = s.split(sep='abc', maxsplit=1)
      # 从右边进行切割
      ret = s.rsplit(' ', maxsplit=1)
      print(ret)
      
      s = 'Hello\nworld'
      # 按照换行进行切割
      print(s.splitlines())
      
      s = 'I love you more than i can say'
      ret = s.split()
      # print(ret)
      # 字符串拼接
      s2 = '*'.join(ret)
      print(s2)
      
      # 输出结果:
      	['I love you more than i can', 'say']
      	['Hello', 'world']
      	I*love*you*more*than*i*can*say
      
    • 字符串查找统计和判断

      s = 'Hi buddy, if you have something to say, than say; if you have nothing to say, than go.'
      
      # 子串查找:找到首次出现的位置,返回下标,找不到返回-1
      ret1 = s.find('hello')
      print(ret1)
      # 从后面查找
      ret2 = s.rfind('to')
      print(ret2)
      # 统计子串出现的次数
      ret3 = s.count('if')
      print(ret3)
      # 判断是否已指定内容开头
      ret4 = s.startswith('Hi')
      print(ret4)
      # 判断是否已指定内容结尾
      ret = s.endswith('go.')
      print(ret)
      
      # 输出结果:
      	-1
      	70
      	2
      	True
      	True
      
    • 字符串转换以及替换

      s = 'hellO worlD!'
      # 转换为全大写
      print(s.upper())
      # 转换为全小写
      print(s.lower())
      # 大小写转换
      print(s.swapcase())
      # 首字母大写
      print(s.capitalize())
      # 每个单词首字母大写
      print(s.title())
      # 用指定的内容替换指定内容,还可以值替换次数
      print(s.replace('l', 'L', 2))
      
      # 输出结果:
      	HELLO WORLD!
      	hello world!
      	HELLo WORLd!
      	Hello world!
      	Hello World!
      	heLLO worlD!
      
    • 字符串类型判断

      s = 'abcABC2'
      # 是否是全大写
      print(s.isupper())
      # 是否是全小写
      print(s.islower())
      # 是否每个单词首字母都大写
      print(s.istitle())
      # 是否是全数字字符
      print(s.isdecimal())
      # 是否是全字母
      print(s.isalpha())
      # 是否全是字母或数字
      print(s.isalnum())
      
      # 输出结果:
      	False
      	False
      	False
      	False
      	False
      	True
      
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值