Python字符串内建函数

Python字符串内建函数

1、find函数:检测字符串是否包括子字符串

str.find(sub, [start,end])

示例代码:

str_example = "hello world I am jinjiaodawang"
print(str_example.find("hello"))   #打印hello第一次出现的下标
print(str_example.find("lll"))     #如果没有出现过 则打印-1

打印结果:

0
1

2、index函数:检测字符串是否包括子字符串

str.index (sub, [start,end])

示例代码:

str_example = "hello world I am jinjiaodawang"
print(str_example.index("hello"))   #打印hello第一次出现的下标
print(str_example.index("no"))      #如果字符串中没有 报异常

打印结果:

0
报异常

3、count函数∶统计字符串中某个字符的个数

str.count(sub, [start,end])

示例代码:

str_example = "hello world I am jinjiaodawang"
print(str_example.count("o"))

打印结果:

3

4、replace函数:将旧字符串替换为新字符串

str.replace(old, new,[count])

示例代码:

str_example = "hello world I am jinjiaodawang"
print(str_example.replace("hello","nohello"))

打印结果:

nohello world I am jinjiaodawang

5、splite函数︰通过指定分隔符对字符串进行切片

str.split(str="", num=string.count(str))

示例代码:

str_example = "hello world I am jinjiaodawang"
print(str_example.split(" ",2))    #以空格为分隔符 实行两次

打印结果:

['hello', 'world', 'I am jinjiaodawang']

6、capitalize :第一个字符大写,其他字符小写

str.capitalize()

示例代码:

str_example = "hello world I am jinjiaodawang"
print(str_example.capitalize())

打印结果:

Hello world i am jinjiaodawang

7、title :所有单词首字母大写,其余字母小写

str.title()

示例代码:

str_example = "hello world I am jinjiaodawang"
print(str_example.title())

打印结果:

Hello World I Am Jinjiaodawang

8、startswith :检查字符串是否以制定子串开头

str.startswith(str, beg=0,end=len(string))

示例代码:

str_example = "hello world I am jinjiaodawang"
print(str_example.startswith("hello"))
print(str_example.startswith("lloheoo"))

打印结果:

True
False

9、endswith :检查字符串是否以制定子串结尾

str.endswith(str, beg=0,end=len(string))

示例代码:

str_example = "hello world I am jinjiaodawang"
print(str_example.endswith("wang"))
print(str_example.endswith("llo"))

打印结果:

True
False

10、upper:将小写字母转为大写字母

str.upper()

示例代码:

str_example = "hello world I am jinjiaodawang"
print(str_example.upper())

打印结果:

HELLO WORLD I AM JINJIAODAWANG

11、lower :将小写字母转为大写字母

str.lower()

示例代码:

str_example = "HELLO world I am jinjiaodawang"
print(str_example.lower())

打印结果:

hello world i am jinjiaodawang

12、ljust:左对齐,使用空格填充至指定长度的新字符串

str.ljust(width,[fillchar])

示例代码:

str_example = "HELLO world I am jinjiaodawang"
print(str_example.ljust(50,"&"))

打印结果:

HELLO world I am jinjiaodawang&&&&&&&&&&&&&&&&&&&&

13、rjust :右对齐,使用空格填充至指定长度的新字符串

str.rjust(width,[fillchar])

示例代码:

str_example = "HELLO world I am jinjiaodawang"
print(str_example.rjust(50,"&"))

打印结果:

&&&&&&&&&&&&&&&&&&&&HELLO world I am jinjiaodawang

14、center :返回一个指定的宽度width居中的字符串

str.center(width,[fillchar])

示例代码:

str_example = "hello world I am jinjiaodawang"
print(str_example.center(50,"-"))

打印结果:

----------HELLO world I am jinjiaodawang----------

15、lstrip:截掉字符串左边的空格或指定字符;
rstrip:截掉字符串右边的空格或指定字符;
strip:截掉字符串左右两边的空格或指定字符

str.lstrip([chars])
str.rstrip([chars])
str.strip([chars])

示例代码:

str_example = "++hello world I am jinjiaodawang++"
print(str_example.lstrip("+"))
print(str_example.rstrip("+"))
print(str_example.strip("+"))

打印结果:

hello world I am jinjiaodawang++
++hello world I am jinjiaodawang
hello world I am jinjiaodawang
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大菜彩

家人们鼓励鼓励!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值