python字符串

字符串

s="abcdef"
字符串删除
del s 

 

访问字符串的值

print(s[0])#a
print(s[1:5])#bcde
print(s[::-1])#fedcba
print(s[:-1])#abcde

 

字符串更新

s="hello"
s=s[:5]+'world'
print(s)#helloworld

 

字符串运算符

a="abc"
b="def"
字符串连接+
print(a+b)#abcdef
字符串重复输出
print(a*2)#abcabc
成员运算符in,not in
print('b' in a)#True
print('d' in a)#False
显示原始字符串r或者R
print(r"abc\n")#abc\n
print(R"abc\n")#abc\n

 

字符串内建函数

1.capitalize()将字符串的第一个字符变大写
s="hello world"
print(s.capitalize())#Hello world

2.center(width, fillchar)返回一个指定的宽度 width 居中的字符串,fillchar 为填充的字符,默认为空格
s="abc"
print(s.center(10,'*'))#***abc****

3.count(str)返回字符出现的次数
s="aabbcde"
print(s.count('a'))#2

4.endswith()检查字符串以什么结束,与之对应的是startswith()
s="abc"
print(s.endswith('c'))#True
print(s.endswith('bc'))#True

5.index(str)检测str是否包含在字符串里,返回索引值,不在就报错
s="hello"
print(s.index('el'))#1

6.find(str)检测str是否包含在字符串里,返回索引值,不在,就返回-1
s="hello"
print(s.find('p'))#-1

7.len()返回字符串长度
s='hello'
print(len(s))#5

8.lstrip()截掉字符串左边的空格或指定字符,strip()执行lstrip()和rstrip()
s="  hello"
print(s.lstrip())#hello
s = "hhhello"
print(s.lstrip('h'))#ello

9.title()所有单词首字母大写
s="hello world"
print(s.title())#Hello World

10.swapcase()将字符串中大写转换为小写,小写转换为大写
s="hello Wo"
print(s.swapcase())#HELLO wO
...................

 

转载于:https://www.cnblogs.com/my12/p/9347328.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值