首页 > 编程 > Python > 正文

python3中str(字符串)的使用教程

2020-02-23 04:29:45
字体:
来源:转载
供稿:网友

本文主要介绍的是python3中对str(字符串)的使用操作总结,文中介绍的非常详细,需要的朋友们下面来一起看看吧。

__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            
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表