从零开始——python学习笔记(字符串操作)

字符串操作
s = “hello”
print(s[0:3])#切片
print(s[:])#截取全部
print(s[::-1])#倒序

#去空格:strip() 消除字符串两端的空格
s_1 = " hello"
print(s_1.strip())
print(s_1.lstrip()) #去左空格
print(s_1.rstrip()) #去右空格

#字符串复制
s_copy = s
print(id(s)) # 不改变原id
print(id(s_copy))

s.title() #首字母大写
print(s.title())
print(s_copy)

#字符串拼接
s1 = “python”
s2 = s + s1
print(s2)
#或者引入 operator
import operator
s2 = operator.concat(s,s1)
print(s2)
#求字符串长度
print(len(s1),len(s2))

#求最大字符,最小字符
print(max(s))
print(min(s1))

#字符串大小写转换
#主要:
#upper — 全大写
#lower — 全小写
#title — 每个词首字母大写
#capitalize — 首字母大写
#swapcase — 大转小,小转大

#字符串分割
s = “aaed ewrw awweqtw”
ss = s.split(“e”) #不填默认按空格
print(ss)

#字符串连接
s = “hqelloASd how are you”
s0 = “ghjkl”
s2 = s.join(s0)
print(s2)

#字符串内查找find
sl = “today is a fine day”
index = s1.find(“is”,7)
print(index)

#字符串内替换
s3= “today is a fine day”
s = s3.replace(“is”,“are”,2)
print(s)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值