python之字符串【str】

#Auther Bob
#--*--conding:utf-8 --*--

#定义一个str的对象,有下面两种方法
name = 'Bob abc'
job = str('it')
print(type(name))
print(type(job))

#

#查看这个类有哪些方法,或者这个类的所有成员,用dir命令
# print(dir(str))
#['__add__', '__class__', '__contains__', '__delattr__',
# '__dir__', '__doc__', '__eq__', '__format__', '__ge__',
# '__getattribute__', '__getitem__', '__getnewargs__',
# '__gt__', '__hash__', '__init__', '__init_subclass__',
# '__iter__', '__le__', '__len__', '__lt__', '__mod__',
# '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__',
# '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__',
# '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center',
# 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format',
# 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit',
# 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace',
# 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans',
# 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition',
# 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip',
# 'swapcase', 'title', 'translate', 'upper', 'zfill']


#查看一个str中是否包含什么,包含返回true,不包含返回false
result = name.__contains__('a')
print(result)
#False
result = name.__contains__('b')
print(result)
#True

#__getattribute__,反射的时候会用到,这里不做发散


#capitalize,可以实现首字母大写
result = name.capitalize()
print(result)
#Bob abc

#casefold,实现字母全小写
job = 'IT'
result = job.casefold()
print(result)



#center,居中显示,一共占用10个位置,不够的用-来补充
result = job.center(10,'-')
print(result)
#----IT----

result = job.center(10,'*')
print(result)
#****IT****

#count,统计某个字符或者子序列出现的次数,还可以指定超找的起始位置和
#结束位置
job = 'abababacdeaf'
result = job.count('ab')
print(result)
# 3
job = 'abababacdeaf'
result = job.count('a')
print(result)
# 5

job = 'abababacdeaf'
result = job.count('a',3,7)
print(result)

# 2

#encode,把utf8转码成其他的编码格式,下面就转码成gbk
name = '山西小霸王'
result = name.encode('gbk')
print(result)

# b'\xc9\xbd\xce\xf7\xd0\xa1\xb0\xd4\xcd\xf5'

#endswith,判断字符串是否以某个字符或者某个子序列结尾的,也可以加一个起始位置
#和结束位置

job = 'abababacdeaf'
result = job.endswith('af')
print(result)
# True
result = job.endswith('s')
print(result)
# False

result = job.endswith('s',3,7)
print(result)
# False

#startswith,和endswith一样,是以什么开头的

#find,查找字符串是否有某个字符或者子序列,如果找到,则返回索引下标
#如果没有找到,则返回-1
job = 'abababacdeaf'
result = job.find('aba')
print(result)
# 0
result = job.find('x')
print(result)
# -1

#index和find的作用是一样的,如果有则返回第一个字符的下标,
#如果不存在,则会报错
job = 'abababacdeaf'
result = job.index('aba')
print(result)
# 0
# result = job.index('x')
# print(result)
# ValueError: substring not found


#format,字符串的格式化输出,这个0 1 2 3必须按照顺序来写,且format中的
#参数要和里面的一致,且顺序也是一一对应的
job = 'it'
name = 'name is {0},and age is {1},job is {2}'
result = name.format('Bob',23,job)
print(result)
# name is Bob,and age is 23,job is it

#上面的还可以这样写
name = 'name is {name},and age is {age},job is {job}'
result = name.format(name='bob',job='it',age=23)
print(result)
# name is bob,and age is 23,job is it


#下面的判断的方法
# isalnum
#
# isdigit
#
# isspace
#
# istitle
#
# isupper
#
# islower


#join,把一个序列连接起来,也是设置分隔符
name = 'abcdefg'

#指定用空格来连接序列
result = ''.join(name)
print(result)
# abcdefg
#指定用*来连接一个序列
result = '*'.join(name)
print(result)
# a*b*c*d*e*f*g

#ljust,rjust,这2个方法和center是对应的,center是放在中间,
#ljust是放在左边,rjust是放在右边

name = 'adec'
result = name.ljust(15,'-')
print(result)
# adec-----------
result = name.rjust(15,'-')
print(result)
# -----------adec


#lower,把大写字母全部替换为小写字母
name = 'BboBadD'
result = name.lower()
print(result)
# bbobadd

#lstrip,rstrip,这个和strip对应,分别是去掉左边的空格或者右边的空格
name = ' abce xyz '
print(name)
result = name.lstrip()
print(result)
result = name.rstrip()
print(result)

# partition,按照某个字符或者字符串分割字符串,这里的
#命令参数只能是1个,会把一个字符串分为三份
name = 'bobisastudent'
result = name.partition('is')
print(result)

# ('bob', 'is', 'astudent')


#replace,用来替换,后面如果有个数字,如果为1,则只转换第一个
#如果是2,则转换前2个,如果是3,则转换前3个,如果没有则全部替换
name = 'botbisastudent'
result = name.replace('bo','2')
print(result)
# 2bisastudent


result = name.replace('t','T',1)
print(result)
# boTbisastudent
result = name.replace('t','T',2)
print(result)
# boTbisasTudent

result = name.replace('t','T')
print(result)
# boTbisasTudenT

#split,指定字符,分割字符串,最后把结果放到list中
name = 'a|b|c|d'
result = name.rsplit("|")
print(result)
['a', 'b', 'c', 'd']

name = 'botbisastudent'
result = name.rsplit("t")
print(result)
['bo', 'bisas', 'uden', '']

#swapcase,可以把大写转换为小写,把小写转换为大写
name = 'BoBobB'
result = name.swapcase()
print(result)
# bObOBb

#把所有的字符串的首字母都大写
name = 'abc daf aec'
result = name.title()
print(result)
# Abc Daf Aec

#upper,把所有字母转换为大写
name = 'abc daf aec'
result = name.upper()
print(result)
# ABC DAF AEC

你可能感兴趣的:(python之字符串【str】)