__add__函数 (在后面追加字符串)
s1 ='Hello'
s2 = s1.__add__(' boy!')
print(s2)
#输出:Hello boy!
__contains__(判断是否包含某字符串,包含则返回True)
s1 = 'Hello'
result = s1.__contains__('He')
print(result)
#输出:True
__eq__(判断两个字符串是否相同,相同则返回True)
s1 = 'Hello'
s2 = 'How'
result = s1.__eq__(s2)
print(result)
#输出:False
__format__
__getattribute__
__getitem__
__getnewargs__
__ge__ (大于或等于)
print('b'.__ge__('a'))
#输出:True
__gt__(大于)
print('b'.__ge__('a'))
#输出:True
__hash__
__iter__
__len__(返回字符串长度)
print('abc'.__len__())
#输出:3
__le__(小于或等于)
print('b'.__le__('a'))
#输出:False
__lt__(小于)
print('b'.__lt__('a'))
#输出:False
__mod__
__mul__
__new__
__ne__
__repr__
__rmod__
__rmul__
__sizeof__
__str__(返回自已)
print('abc'.__str__())
#输出:abc
capitalize(首字母大写)
s = 'tom'
print(s.capitalize())
#输出:Tom
casefold(大写转换成小写)
s = 'TOM'
print(s.casefold())
#输出:tom
center (指定长度和填充字符,内容居中,填充字符留空则为空格)
s = 'Tom'
print(s.center(20,'-'))
#输出:--------Tom---------
count(计算某个字符串出现的个数,第二个参数:起始位置,第三个参数:结束位置)
s = 'aabbbcccccdd'
print(s.count('cc',3,11))
#输出:2
encode(编码)
s = "中文"
print(s.encode('gbk'))
#输出:b'\xd6\xd0\xce\xc4'
endswith(判断字符串是否以某个字符或字符串结尾的,第二个参数:起始位置,第三个参数:结束位置)
s = 'Projects'
print(s.endswith('ts'))
print(s.endswith('e',0,5))
#输出:True
# True
expandtabs(把1个tab键转换成7个空格)
s = 'H\ti'
print(s.expandtabs())
#输出:H i
find(查找某个字符或字符串的索引位置,第二个参数:起始位置,第三个参数:结束位置)
s = 'Hello'
print(s.find('o'))
print(s.find('o',0,3)) #找不到返回-1
#输出:4
# -1
format(字符串格式化/拼接)
name = 'Tom'
age = 18
s = '{0}\'s age is {1}'.format(name,age)
print(s)
#或者
str = '{name}\'s age is {age}'
result = str.format(age=18,name='Tom')
print(result)
#输出:Tom's age is 18
format_map
index(查找某个字符或字符串的索引位置,和find不一样是,如果字符不存在,会报错)
s = 'Hello'
print(s.index('o'))
print(s.index('e',0,3))
#输出:4
# 1
isalnum(是否为字母或数字)
s = '!#'
print(s.isalnum())
#输出:False
isalpha(是否为字母)
s = '123'
print(s.isalpha())
#输出:False
isdecimal(是否为十进制数)
s = '123'
print(s.isdecimal())
#输出:True
#True: Unicode数字,,全角数字(双字节)
#False: 罗马数字,汉字数字
#Error: byte数字(单字节)
isdigit(是否为数字)
s = '123'
print(s.isdigit())
#输出:True
#True: Unicode数字,byte数字(单字节),全角数字(双字节),罗马数字
#False: 汉字数字
isidentifier(是否为标识符/变量名)
s = '1num'
print(s.isidentifier())
#输出:False
#因为变量名不能以数字开头
islower(是否全部为小写字母)
s = 'Hello'
print(s.islower())
#输出:False
isnumeric(是否为数字)
s = '123'
print(s.isnumeric())
#输出:True
#True: Unicode数字,全角数字(双字节),罗马数字,汉字数字
isprintable(是否为可打印字符/能否原样输出)
s = '\n'
print(s.isprintable())
#输出:False
isspace(是否为空格)
print(' '.isspace())
print('\t'.isspace())
#输出:True
# True
istitle(是否为标题/每个单词首字母大写)
print('Hello Boy'.istitle())
print('hello boy'.istitle())
#输出:True
# False
isupper(是否全部为大写字母)
print('BOY'.isupper())
print('Boy'.isupper())
#输出:True
# False
join(将序列中的元素以指定的字符连接生成一个新的字符串)
s = ['H','e','l','l','o']
print(''.join(s))
print('-'.join(s))
#输出:Hello
# H-e-l-l-o
ljust(指定长度和填充字符,内容左对齐,填充字符留空则为空格)
s = 'Hello'
print(s.ljust(10,'-'))
#输出:Hello-----
lower(字符串全部换成小写)
s = 'TOM'
print(s.lower())
#输出:t