加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python_字符串的操作

发布时间:2020-12-17 00:10:34 所属栏目:Python 来源:网络整理
导读:一:字符串的方法与操作 *注意:首字母为l的为从左边操作,为r的方法为从右边操作 1.__contains__()判断是否包含 判断指定字符或字符串是否包含在一个字符串内,返回值为true或者false str1= span style="color: #0000ff"print (str1.span style="color: #800

一:字符串的方法与操作

*注意:首字母为l的为从左边操作,为r的方法为从右边操作

1.__contains__()判断是否包含

判断指定字符或字符串是否包含在一个字符串内,返回值为true或者false

str1=

<span style="color: #0000ff">print(str1.<span style="color: #800080">contains(<span style="color: #800000">'<span style="color: #800000">a<span style="color: #800000">'<span style="color: #000000">))

<span style="color: #0000ff">print(str1.<span style="color: #800080">contains(<span style="color: #800000">"<span style="color: #800000">df<span style="color: #800000">"<span style="color: #000000">))

<span style="color: #0000ff">print(str1.<span style="color: #800080">contains(<span style="color: #800000">'<span style="color: #800000">r<span style="color: #800000">'))

运行结果:

True

True

False

作用和in相似

str1=

<span style="color: #0000ff">print(<span style="color: #800000">'<span style="color: #800000">s<span style="color: #800000">' <span style="color: #0000ff">in str1)

运行结果:

True

2.__eq__()相等

判断两个字符串是否相等,返回值为True或者False

str1=

<span style="color: #0000ff">print(str1.<span style="color: #800080">eq(<span style="color: #800000">"<span style="color: #800000">asdf<span style="color: #800000">"<span style="color: #000000">))

<span style="color: #0000ff">print(str1.<span style="color: #800080">eq(<span style="color: #800000">"<span style="color: #800000">addfd<span style="color: #800000">"))

运行结果:

True

False

3.字符串相加可以用%s+

str1=str2=<span style="color: #800000">"<span style="color: #800000">fgh<span style="color: #800000">"<span style="color: #000000">

str3=str1+<span style="color: #000000">str2

str4=<span style="color: #800000">"<span style="color: #800000">%s%s<span style="color: #800000">"%<span style="color: #000000">(str1,str2)

<span style="color: #0000ff">print<span style="color: #000000">(str3)

<span style="color: #0000ff">print(str4)

运行结果:

"asdfgh"

"asdfgh"

4.format字符串拼接

str1=result=str1.format(<span style="color: #800000">"<span style="color: #800000">hu<span style="color: #800000">",<span style="color: #800000">"<span style="color: #800000">ui<span style="color: #800000">"<span style="color: #000000">)

<span style="color: #0000ff">print<span style="color: #000000">(result)

str2=<span style="color: #800000">"<span style="color: #800000">as{id}dsfdfz{name}<span style="color: #800000">"<span style="color: #000000">

result=str2.format(id=<span style="color: #800000">"<span style="color: #800000">hu<span style="color: #800000">",name=<span style="color: #800000">"<span style="color: #800000">ui<span style="color: #800000">"<span style="color: #000000">)

<span style="color: #008000">#<span style="color: #008000">在format里面的变量不能到外面去使用

<span style="color: #0000ff">print(result)

运行结果:

"ashudszui"

"ashudsfdfzui"

5.capitalize()字符串首字母大写

str1=

<span style="color: #0000ff">print(str1.capitalize())

运行结果:

"Asdfg"

6.casefold()首字母小写

str1=

<span style="color: #0000ff">print(str1.capitalize())

运行结果:

"aSDFG"

7.center()把内容居中 两个参数

=

<span style="color: #0000ff">print(str1.center(20<span style="color: #000000">))

<span style="color: #0000ff">print(str1.center(30,<span style="color: #800000">'<span style="color: #800000">*<span style="color: #800000">'))

运行结果

        sdfg        *************sdfg*************

8.encode()编码

str1=<span style="color: #800000">"<span style="color: #800000">兰艳茹<span style="color: #800000">"

<span style="color: #0000ff">print(str1.encode(<span style="color: #800000">"<span style="color: #800000">gbk<span style="color: #800000">"))

运行结果:

b'xc0xbcxd1xdexc8xe3'

9.endswith() 判断一个字符串是否是以某个字符结尾

str1=

<span style="color: #0000ff">print(str1.endswith(<span style="color: #800000">'<span style="color: #800000">h<span style="color: #800000">'<span style="color: #000000">))

<span style="color: #0000ff">print(str1.endswith(<span style="color: #800000">'<span style="color: #800000">e<span style="color: #800000">'))

运行结果:

TrueFalse

10.expandtabs()把tab转换成空格t

*自我认为没有什么卵用

str1=

<span style="color: #0000ff">print<span style="color: #000000">(str1)

<span style="color: #0000ff">print(str1.expandtabs())

运行结果:

sdfdf  1wssdfdf   1ws

11.find查找某个字符在字符串里面的位置,没有的话显示-1,可以加上起始位置和结束位置

str1=

<span style="color: #0000ff">print(str1.find(<span style="color: #800000">'<span style="color: #800000">h<span style="color: #800000">'<span style="color: #000000">))

<span style="color: #0000ff">print(str1.find(<span style="color: #800000">'<span style="color: #800000">a<span style="color: #800000">'))

运行结果:

4-1

12.index 返回位置

返回字符在字符串中的位置,没有找到的话就报错

str1=

<span style="color: #0000ff">print(str1.index(<span style="color: #800000">'<span style="color: #800000">h<span style="color: #800000">'<span style="color: #000000">))

<span style="color: #0000ff">print(str1.index(<span style="color: #800000">'<span style="color: #800000">a<span style="color: #800000">'))

运行结果:

4Traceback (most recent call last):
  File "/usercode/file.py",line 8,in 
    print(str1.index('a'))
ValueError: substring not found

13.join()用来拼接,""代表分隔符,可以定义

str1=[,,,<span style="color: #0000ff">print(<span style="color: #800000">""<span style="color: #000000">.join(str1))

<span style="color: #0000ff">print<span style="color: #000000">(str1)

<span style="color: #0000ff">print(<span style="color: #800000">"<span style="color: #800000">-<span style="color: #800000">".join(str1))

运行结果:

song['s','o','n','g']s-o-n-g

14.ljust()放到左边,同center

像center一样,把字符串在一行中进行定位,ljust是从左面开始定位,参数为自左开始的长度

str1=

<span style="color: #0000ff">print(str1.ljust(10,<span style="color: #800000">'<span style="color: #800000">+<span style="color: #800000">'<span style="color: #000000">’))

<span style="color: #0000ff">print(str1.ljust(20,<span style="color: #800000">'<span style="color: #800000">-<span style="color: #800000">'<span style="color: #000000">))

<span style="color: #0000ff">print(str1.ljust(30<span style="color: #000000">))

<span style="color: #0000ff">print(str1.ljust(30,<span style="color: #800000">'<span style="color: #800000">*<span style="color: #800000">'))

运行结果:

qeretry+++qeretry-------------qeretry                       qeretry***********************

15.lower()小写

全部小写

str1=

<span style="color: #0000ff">print<span style="color: #000000">(str1.lower())

<span style="color: #0000ff">print(str1)

运行结果:

asdfgdAsdFGd

16.lstrip()去除左边空格

str1=

<span style="color: #0000ff">print(str1.lstrip())

运行结果:

ddfd  

17.maketrans()与translate()方法

这两个方法需要进行对比联合起来使用

str1=str2=<span style="color: #800000">"<span style="color: #800000">asdfg<span style="color: #800000">"<span style="color: #000000">
aa=<span style="color: #800000">"<span style="color: #800000">afgjdfhd<span style="color: #800000">"<span style="color: #000000">

makes=<span style="color: #000000">aa.maketrans(str2,str1)

<span style="color: #0000ff">print(aa.translate(makes))

运行结果:

145j34h3

18.partition(“分割的字符”)分割

str1=

<span style="color: #0000ff">print(str1.partition(<span style="color: #800000">"<span style="color: #800000">ai<span style="color: #800000">"))

运行结果:

('wo','ai','python')

19.replace()替换

  name.replace('老字符','新字符')  name.replace('老字符','新字符',‘转换几个’)

str1=

<span style="color: #0000ff">print(str1.replace(<span style="color: #800000">'<span style="color: #800000">a<span style="color: #800000">',<span style="color: #800000">'<span style="color: #800000">p<span style="color: #800000">'<span style="color: #000000">))

<span style="color: #0000ff">print(str1.replace(<span style="color: #800000">'<span style="color: #800000">a<span style="color: #800000">',<span style="color: #800000">'<span style="color: #800000">q<span style="color: #800000">',3))

运行结果:

psdfghjklpdpdpfgpspgqsdfghjklqdqdafgasag

20.rfind()

运用方法同find一样,区别就是自右向左查找

21.rjust()

运用方法同上面的ljust一样,区别就是自右向左查找

22.rsplit() 指定字符,分割字符串

被指定的字符会被删除

str1=

<span style="color: #0000ff">print(str1.rsplit(<span style="color: #800000">'<span style="color: #800000">s<span style="color: #800000">'))

运行结果:

['qwetatra','','ong','dchengxcxu']

23.splitlines()根据换行符进行分割,等同于split('n')

str1= = (str1.splitlines())

运行结果:

['"aa""bb""cc"']['"aa"','"bb"','"cc"']

24.startswith()以什么开头

判断字符串是否是以什么字符或字符串开头

str1=

<span style="color: #0000ff">print(str1.startswith(<span style="color: #800000">'<span style="color: #800000">a<span style="color: #800000">'<span style="color: #000000">))

<span style="color: #0000ff">print(str1.startswith(<span style="color: #800000">"<span style="color: #800000">ad<span style="color: #800000">"<span style="color: #000000">))

<span style="color: #0000ff">print(str1.startswith(<span style="color: #800000">"<span style="color: #800000">ddd<span style="color: #800000">"))

运行结果:

TrueTrueFalse

25.swapcase()大小写转换,大变小,小变大

str1=

<span style="color: #0000ff">print(str1.swapcase())

运行结果:

DSddFfdsssssffQQQ

26.title()把字符串转换成标题,即首字母大写

str1=

<span style="color: #0000ff">print(str1.title())

运行结果:

Dkjgdkgj

二:总结

  1.常用方法

  center(),startswith(),ljust(),rjust(),__eq__(),partition(),replace(),rsplit(),splitlines(),lstrip(),rstrip(),strip(),join(),index(),format()

  2.注意养成习惯:无论是元组,列表,还是字典,在元素后面加上逗号eg:str=['1','a',]

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读