码迷,mamicode.com
首页 > 其他好文 > 详细

字符串常见操作

时间:2017-03-14 23:50:07      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:and   字符串   dig   count   bsp   分割   lin   返回   长度   

如有字符串mystr = ‘hello world loaderman and loadermancpp‘,以下是常见的操作

<1>find

检测 str 是否包含在 mystr中,如果是返回开始的索引值,否则返回-1

mystr.find(str, start=0, end=len(mystr))

 Demo1

mystr = "hello world loaderman and loadermancpp"
result= mystr.find("itcast", 0, 10)
print(result)

 运行结果:-1

Demo2

mystr = "hello world loaderman and loadermancpp"
result= mystr.find("loaderman")
print(result)

 运行结果:12

<2>index

跟find()方法一样,只不过如果str不在 mystr中会报一个异常.

mystr.index(str, start=0, end=len(mystr)) 

 Demo3

mystr = "hello world loaderman and loadermancpp"
result= mystr.index("loaderman",0,10)
print(result)

 运行结果: ValueError: substring not found

<3>count

mystr.count(str, start=0, end=len(mystr))

 

返回 str在start和end之间 在 mystr里面出现的次数

demo4

mystr = "hello world loaderman and loadermancpp"
result = mystr.count("loaderman",0,10)
print(result)

 运行结果:0

Demo5

mystr = "hello world loaderman and loadermancpp"
result = mystr.count("loaderman")
print(result)

 运行结果:2

<4>replace

mystr.replace(str1, str2,  mystr.count(str1))

把 mystr 中的 str1 替换成 str2,如果 count 指定,则替换不超过 count 次.

Demo6

mystr = "hello world loaderman and loadermancpp"
result = mystr.replace("loaderman","LoaderMan",2)
print(result)

 运行结果: hello world LoaderMan and LoaderMancpp

<5>split

mystr.split(str=" ", 2)  

以 str 为分隔符切片 mystr,如果 maxsplit有指定值,则仅分隔 maxsplit 个子字符串

Demo7

mystr = "hello world loaderman and loadermancpp"
result = mystr.split(" ")
print(result)

 运行结果: [‘hello‘, ‘world‘, ‘loaderman‘, ‘and‘, ‘loadermancpp‘]

Demo8

mystr = "hello world loaderman and loadermancpp"
result = mystr.split(" " , 2)
print(result)

 运行结果:[‘hello‘, ‘world‘, ‘loaderman and loadermancpp‘]

<6>capitalize

mystr.capitalize()

把字符串的第一个字符大写

Demo9

mystr = "hello world loaderman and loadermancpp"
result = mystr.capitalize()
print(result)

 运行结果:Hello world loaderman and loadermancpp

<7>startswith

mystr.startswith(obj)

检查字符串是否是以 obj 开头, 是则返回 True,否则返回 False

 Demo10

mystr = "hello world loaderman and loadermancpp"
result = mystr.startswith("hello")
print(result)

 运行结果:True

8>endswith

mystr.endswith(obj)

检查字符串是否以obj结束,如果是返回True,否则返回 False

Demo11

mystr = "hello world loaderman and loadermancpp"
result = mystr.endswith("hello")
print(result)

 运行结果:False

<9>lower

mystr.lower()   

转换 mystr 中所有大写字符为小写

Demo12

mystr = "hello world LoaderMan and loadermancpp"
result =mystr.lower()  
print(result)

 运行结果:hello world loaderman and loadermancpp

<10>upper

mystr.upper()   

转换 mystr 中的小写字母为大写

Demo13

mystr = "hello world LoaderMan and loadermancpp"
result =mystr.upper()  
print(result)

 运行结果:HELLO WORLD LOADERMAN AND LOADERMANCPP

<11>ljust

mystr.ljust(width) 

返回一个原字符串左对齐,并使用空格填充至长度 width 的新字符串

mystr = "hello"
result =mystr.ljust(10) 
print(result)

 运行结果:‘hello    ‘

<12>rjust

mystr.rjust(width) 

返回一个原字符串右对齐,并使用空格填充至长度 width 的新字符串

mystr = "hello"
result =mystr.ljust(10) 
print(result)

 运行结果:‘    hello‘

<13>center

mystr.center(width)  

返回一个原字符串居中,并使用空格填充至长度 width 的新字符串

mystr = "hello"
result =mystr.center(10) 
print(result)

 运行结果:‘    hello   ‘

<14>lstrip

mystr.lstrip()

删除 mystr 左边的空格

<15>rstrip

mystr.rstrip()  

删除 mystr 字符串末尾的空格

<16>rfind

mystr.rfind(str, start=0,end=len(mystr) )

类似于 find()函数,不过是从右边开始查找.

<17>rindex

mystr.rindex( str, start=0,end=len(mystr))

类似于 index(),不过是从右边开始.

<18>partition

mystr.partition(str)

把mystr以str分割成三部分,str前,str和str后

<19>rpartition

mystr.rpartition(str)

类似于 partition()函数,不过是从右边开始.

<20>splitlines

mystr.splitlines() 

按照行分隔,返回一个包含各行作为元素的列表

<21>isalnum

mystr.isalnum()  

如果 mystr 所有字符都是字母或数字则返回 True,否则返回 False

<22>isalpha

mystr.isalpha()  

如果 mystr 所有字符都是字母 则返回 True,否则返回 False

<23>isdigit

mystr.isdigit() 

<24>isspace

mystr.isspace()   

如果 mystr 中只包含空格,则返回 True,否则返回 False.

如果 mystr 只包含数字则返回 True 否则返回 False.

<25>isupper

mystr.isupper() 

如果 mystr 所有字符都是大写,则返回 True,否则返回 False

<26>join

mystr.join(str)

mystr 中每个字符后面插入str,构造出一个新的字符串

字符串常见操作

标签:and   字符串   dig   count   bsp   分割   lin   返回   长度   

原文地址:http://www.cnblogs.com/loaderman/p/6551170.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!