python-str对象内置方法

capitalize:将字符串的第一个字符转换为大写,返回一个新的字符串
title:将字符首字母转为大写,其余小写,注意和capitalize区别
casefold:将字符串都转为小写,返回一个新的字符串
lower:将str对象中的字符转为小写,同casefold方法
upper:将str对象中的字符转为大写
swapcase:将str对象中的大写转为小写,小写字符转为大写

>>> 'a bc'.capitalize()
'A bc'
>>> 'a bc'.title()
'A Bc'
>>> 'Aa Bbc'.casefold()
'aa bbc'
>>> 'Aa Bbc'.lower()
'aa bbc'
>>> s="Hello"
>>> s.upper()
'HELLO'
>>> "Hello".swapcase()
'hELLO'

center(width,fillchar):返回width长度的字符串,原str居中,默认以空格填充
ljust(width,fillchar):返回width长度的字符串,原str居左,默认以空格填充
rjust(width,fillchar):返回width长度的字符串,原str居右,默认以空格填充
zfill(width):返回width长度,以0填充

>>> Str="efgabc"
>>> Str.center(10)
'  efgabc  '
>>> Str.center(10,"*")
'**efgabc**'
>>> s='ab'
>>> s.ljust(10)
'ab        '
>>> s.ljust(10,"*")
'ab********'
>>> s.rjust(10,"*")
'********ab'
>>> s.zfill(10)
'00000000ab'

split(str,num):str指定分隔字符串,默认是空格,num指定分隔次数,默认是-1表示分隔所有,返回分隔后的列表
rsplit(str,num):str指定分隔字符串,默认是空格,num指定分隔次数,默认是-1表示分隔所有,从右侧开始分隔,返回分隔后的列表
splitlines(keepends):按换行回车字符(\r,\n,\r\n)进行分隔,keepends为true保留分隔符,默认为否,返回分隔后的列表
partition:分隔字符串,返回一个三个元素的元组,若存在指定的分隔符(匹配从左边开始匹配的第一个字符),第一个为分隔符左边的子串,第二个为分隔符本身,第三个为分隔符右边的子串
rpartition:分隔字符串,返回一个三个元素的元组,若存在指定的分隔符(匹配从右边开始匹配的第一个字符),第一个为分隔符左边的子串,第二个为分隔符本身,第三个为分隔符右边的子串

>>> s=' hello '
>>> s.split('l',1)
[' he', 'lo  ']
>>> s.split('l')
[' he', '', 'o  ']
>>> s.rsplit('l',1)
[' hel', 'o  ']
>>> "a\r b\r\n c\n d".splitlines()
['a', ' b', ' c', ' d']
>>> "a\r b\r\n c\n d".splitlines(True)
['a\r', ' b\r\n', ' c\n', ' d']
>>> s="Hello"
>>> s.partition("c")
(' hello  ', '', '')
>>> s.partition("l")
(' he', 'l', 'lo  ')
>>> s.rpartition("l")
(' hel', 'l', 'o  ')

find(str,start,end):查找字符串中指定字符串的第一次出现的下标位置,start,end表示查找范围,若查不到对应的字符串返回-1
rfind(str,start,end):查找字符串中指定字符串的最后次出现的下标位置,start,end表示查找范围,若查不到对应的字符串返回-1
index(str,start,end):同find方法
rindex(str,start,end):同rfind方法

>>> s='hello,python'
>>> s.find("python")
6
>>> s.find("d")
-1
>>> s.rfind('h')
9

lstrip:去掉str的左空格
strip:去掉str的左右空格
rstrip:去掉str的右空格

>>> s="Hello"
>>> s.lower()
'hello'
>>> s.upper()
'HELLO'
>>> "Hello".swapcase()
'hELLO'
>>> 'hello'.title()
'Hello'
>>> s=" hello  "
>>> s.lstrip()
'hello  '
>>> s.strip()
'hello'

format:格式化输出字符串,使用方法地址
format_map:功能类似format
join(seq):使用str连接seq
encode(encoding='utf-8'):以指定编码格式编码str
count(sub,start,end):统计str中字符串sub出现的次数,若不存在则返回0,strat和end默认为None
endswith(suffix,start,end):检查是否已suffix结尾,是则返回True,否则返回False,start和end指定检查范围
startswith(str):字符串对象有str开头的则返回True,否则返回false
replace(old,new,num):替换字符串,num表示最多替换的次数,默认时-1,表示所有
expandtabs(num):替换字符串中的tab字符,默认是8个空格

>>> '-'.join("abc")
'a-b-c'
>>>Str="abcef"
>>> Str.count('ab')
1
>>> Str.count('e')
1
>>> Str.count('ek')
0
>>> "测试".encode()
b'\xe6\xb5\x8b\xe8\xaf\x95'

>>> Str.endswith('a')
False
>>> Str.endswith('ef')
True
>>> s=' hello '
>>> s.startswith("\x20")   由于s字符串是以空格开头的,所以匹配是空格返回true,h字符匹配失败返回false
True
>>> s.startswith("h")
False
>>> 'hello python'.replace('h','HH')
'HHello pytHHon'
>>> 'hello python'.replace('h','HH',1)
'HHello python'
>>> s='a        b               c'
>>> s.expandtabs(2)
'a b   c'
>>> s.expandtabs(0)
'abc'

isalnum:判断字符串中的字符全是数字则返回true,否则返回false
isalpha:判断字符串中的字符全是字符则返回true,否则返回false
islower: 判断字符串中的字符全是小写则返回true,否则返回false
isupper:: 判断字符串中的字符全是大写则返回true,否则返回false
其他类似方法:
isassci isdecimal isdigit isidentifier isnumeric isprintable isspace istitle

>>> s='hello,python'
>>> s.isalnum()
False
>>> s2='123'
>>> s2.isalnum()
True
>>> s3='abc'
>>> s.isalpha()
False
>>> s3.isalpha()
True
>>> s.islower()
True
>>> s2="Abc"
>>> s2.islower()
False
>>> s.isupper()
False
>>> s2.isupper()
False
>>> s3='ABD'
>>> s3.isupper()
True

你可能感兴趣的:(python,学习笔记,总结,python,字符串)