码迷,mamicode.com
首页 > 编程语言 > 详细

Python(8)--字符串方法

时间:2018-11-25 17:59:00      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:erro   make   join   大写   标准化   结束   ref   lin   swa   

center(width[, fillchar])        指定字符串长度,在两边填充字符(默认空格),让字符串居中

>>> "hello".center(30)
‘            hello             ‘
>>> "hello".center(30, "#")
‘############hello#############‘

  

ljust(width,[, fillchar])        指定字符串长度,在两边填充字符(默认空格),让字符串左对齐

>>> "hello".ljust(30)
‘hello                         ‘
>>> "hello".ljust(30, "#")
‘hello#########################‘

  

rjust(width,[, fillchar])        指定字符串长度,在两边填充字符(默认空格),让字符串右对齐

>>> "hello".rjust(30)
‘                         hello‘
>>> "hello".rjust(30, "#")
‘#########################hello‘

  

zfill(width)        指定字符串长度,在左边填充0,将+或者-移动到开头

>>> "+3.14".zfill(+30)
‘+00000000000000000000000003.14‘
>>>
>>> "+3.14".zfill(30)
‘+00000000000000000000000003.14‘
>>> "-3.14".zfill(30)
‘-00000000000000000000000003.14‘
>>> "hello".zfill(30)
‘0000000000000000000000000hello‘

  

find(sub[, start[, end]])        查找字符串中的子串,可以指定查找范围,找到就返回第一个子串的索引,没找到返回-1

>>> "hellohiworldhi".find("hi")
5
>>> "hellohiworldhi".find("hi", 7, 14)
12
>>> "hellohiworldhi".find("hi", 7, 8)
-1

  

rfind(sub[, start[, end]])       同find,但找到后返回的是最后一个子串的索引

>>> "hellohiworldhi".rfind("hi")
12

  

index(sub[, start[, end]])        查找字符串中的子串,可以指定查找范围,找到就返回第一个子串的索引,没找到引发ValueErroe异常

>>> "hellohiworldhi".index("hi")
5
>>> "hellohiworldhi".index("hi", 7, 8)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: substring not found

  

rindex(sub[, start[, end]])        同index,但找到后返回的是最后一个子串的索引

>>> "hellohiworldhi".rindex("hi")
12

  

count(sub[, start[, end]])        计算子串sub出现的次数,可以指定查找范围

>>> "hellohiworldhi".count("hi")
2
>>> "hellohiworldhi".count("hi", 7, 8)
0

  

startswith(prefix[, start[, end]])        检查字符串是否以perfix开头,可以指定查找范围

>>> "hellohiworldhi".startswith("hi")
False
>>> "hellohiworldhi".startswith("h")
True
>>> "hellohiworldhi".startswith("h", 5)
True

 

endswith(suffix[, start[, end]])       检查字符串是否以suffix结尾,可以指定查找范围

 

join(seq)        将字符串与序列seq合并,返回结果

>>> "+".join("hello")
‘h+e+l+l+o‘
>>>
>>> seq = "C","admin","bin","test"
>>> "/".join(seq)
‘C/admin/bin/test‘

  

split([sep[, maxsplit]])        以sep为分隔符将字符串进行分割,得到列表,默认是空白分隔,可以指定最大分隔次数

>>> "a b c d e f".split()
[‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘]
>>> "a b c d e f".split(" ",2)
[‘a‘, ‘b‘, ‘c d e f‘]
>>> "1,2,3,4,5".split(",")
[‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘]

  

rsplit([sep[, maxsplit]])        与split相同,从右往左进行分割

 

lower()        将字符串字母变小写,返回结果

>>> "Hello WorlD".lower()
‘hello world‘

   

title()        将字符串中所有单词首字母大写,返回结果

>>> "helloworld".title()
‘Helloworld‘
>>> "hello world".title()
‘Hello World‘

   

upper()        将字符串中所有字母变大写,返回结果

>>> "hello".upper()
‘HELLO‘

  

maketrans(x[, y[, z]])        静态方法,创建一个供translate使用的转换表,只传x,x必须是从字符或者序列到Unicode序数或者None(用户删除)的映射;也可以使用两个表示源字符和目标字符的字符串;第三个参数用于指定要删除的字符

translate(table)        根据转换表table(通过maketrans创建)对字符串中的所有字符进行转换,返回结果

# 两个参数是长度相同的字符串,指定要将第一个字符串的每隔字符都替换为第二个字符串中的相应字符
>>> table = str.maketrans("cv", "as")

# 转换表的内容是Unicode码点的映射
>>> table
{99: 97, 118: 115}

# 将表作为translate的参数
>>> "hello cv !!".translate(table)
‘hello as !!‘

# 添加第三个参数表示要删除的字符,这里添加空格,将空格都删除
>>> table = str.maketrans("cv", "as", " ")
>>> "hello cv !!".translate(table)
‘helloas!!‘

  

capitalize()        返回字符串的副本,当地一个字符大写

>>> "hello".capitalize()
‘Hello‘

  

casefold()        返回经过标准化后的字符串,类似于转换为写哦啊写,更适用于Unicode字符串进行不区分大小写的比较

>>> "HeLLO".casefold()
‘hello‘

  

swapcase()        将字符串的所有字母大小写反转。返回结果

>>> "HeLLO".swapcase()
‘hEllo‘

  

replace(old, new[, max])        将字符串中的子串old替换为new,可以指定替换次数,返回结果

>>> "HeLLO".replace("L", "&")
‘He&&O‘
>>> "HeLLO".replace("L", "&", 1)
‘He&LO‘

  

strip([chars])        将字符串开头和结尾的所有指定的字符都删除(默认是所有空白字符)

>>> "    hello    ".strip()
‘hello‘
>>> "####hello##".strip("#")
‘hello‘

  

lstrip([chars])        同strip,删除的是开头字符

rstrpi([chars])        同strip,删除的是结束字符

 

判断字符串是否满足特定条件:

isalnum()        检查字符串的字符是否都是字母或数字

isalpha()        检查字符串的字符是否都是字母

isdecimal()        检查字符串的字符是否都是十进制数

isdigit()        检查字符串的字符是否都是数字

isidentifier()        检查字符串是否用作Python标识符

islower()        检查字符串中字母是否都是小写

isnumeric()        检查字符串的字符是否都是数字字符

isprintable()        检查字符串的字符是否都是可以打印的

isspace()        检查字符串的字符是否都是空白字符

istitle()        检查字符串非字母后面单词首字母是否都是大写,其他字母小写

isupper()        检查字符串中字母是否都是大写

 

Python(8)--字符串方法

标签:erro   make   join   大写   标准化   结束   ref   lin   swa   

原文地址:https://www.cnblogs.com/Mr-chenshuai/p/10015747.html

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