python_str

字符串的常用函数

capitalize(self)
S.capitalize() -> string
返回一个字符串的copy,并且将第一个字母大写
s = "keep on "
print (s.capitalize())
result > Keep on 
center(self, width, fillchar=None)
S.center(width[, fillchar]) -> string
返回一个总长度为指定长度的字符串,并且将S居中,左右填充指定字符,默认为空格
print (s.center(10,"*"))
result > ***keep***
count(self, sub, start=None, end=None)
S.count(sub[, start[, end]]) -> int
查询指定的子字符串的个数,可以指定开始,结束位置
print (s.count("e",0,2))
result > 1
endswith(self, suffix, start=None, end=None)
S.endswith(suffix[, start[, end]]) -> bool
返回一个布尔类型,判断在指定长度内,是不是以某个标识结尾
 print (s.endswith("*"))
 result > false
expandtabs(self, tabsize=None)
S.expandtabs([tabsize]) -> string
返回一个字符串的copy,并将其中的tab转化为空格,默认是8个空格 
s = "keep\ton"
print (s.expandtabs(4))
result >keep    on
def find(self, sub, start=None, end=None)
S.find(sub [,start [,end]]) -> int
返回最开始的指定字符串的index
s="keep"
print (s.find("e",0,4))
result > 1
format(self, *args, **kwargs)
  S.format(*args, **kwargs) -> string
  字符串格式化
  Return a formatted version of S, using substitutions from args and kwargs.
        The substitutions are identified by braces ('{' and '}')
s="hello,{0},hi,{1}"
print (s.format("tom","jony"))
result >hello,tom,hi,jony
 def isalnum(self)
 S.isalnum() -> bool
 判断这个字符串是不是数字或者字母
def isalpha(self)
S.isalpha() -> bool
判断是不是字母
def isdigit(self)
判断是不是数字
def islower(self)
判断是不是小写 
def isspace(self)
判断是不是空格
def istitle(self)
判断是不是标题(标题的单词第一个字母大写)
def isupper(self)
判断是不是大写
def join(self, iterable)
S.join(iterable) -> string
将S作用于可迭代对象,将他们使用S链接起来
li = ["a" ,"b"]
s = "and"
print (s.join(li))
result > aandb
def ljust(self, width, fillchar=None)
S.ljust(width[, fillchar]) -> string
字符串左对齐,右侧填充
s = "test"
print (s.ljust(10,"*"))
result > test******
def lower(self)
def upper(self)
def lstrip(self, chars=None)
S.lstrip([chars]) -> string or unicode
去掉左空格
def partition(self, sep)
S.partition(sep) -> (head, sep, tail)
找到S中的分隔符,返回前面的,自己,后边的
print (s.partition("e"))
result > ('t', 'e', 'st')
def replace(self, old, new, count=None)
S.replace(old, new[, count]) -> string
返回一个字符串的copy ,并用新字符串替换旧的
print (s.replace("e","L",1))
result >tLst 
def rsplit(self, sep=None, maxsplit=None)
S.rsplit([sep [,maxsplit]]) -> list of strings
print (s.split("e",2))
result >  ['t', 'st']
def splitlines(self, keepends=False)
S.splitlines(keepends=False) -> list of strings
换行分割
s = "test\nheelo"
print (s.splitlines())
result > ['test', 'heelo']
def startswith(self, prefix, start=None, end=None)
S.startswith(prefix[, start[, end]]) -> bool
 def swapcase(self)
 S.swapcase() -> string
 大写换小写,小些转大写
 def title(self)
 S.title() -> string
 将字符串转换为标题
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值