字符串str内部功能
'''
什么是字符串
字符串是以单引号或双引号括起来的任意文本
'abc'
"def"
字符串不可变
'''
'''
字符串运算
'''
#字符串链接
str6 = "sunck is a"
str7 = "good man"
str8 = str6 + str7
print("str6 =",str6)
print("str7 =",str7)
print("str8 =",str8)
结果:
str6 = sunck is a
str7 = good man
str8 = sunck is agood man
#输出重复字符串
str9 = "good"
str10 = str9 * 3
print("str10 = ", str10)
结果:str10 = goodgoodgood
#访问字符串中的某一个字符.索引从0开始
#字符串名[下标]
str11 = "sunck is a good man!"
print(str11[1])
#str11[1] = 'a' #字符串不可变
#print("str = ", str11)
结果:u
#截取字符串中的一部分
str13 = "sunck is a good man!"
#从给定下标出开始截取到给定下标之前
str15 = str13[6:15]
print(str15)
#从头截取到给定下标之前
str16 = str13[:5]
print(str16)
#从给定下标出到结尾
str17 = str13[16:]
print("str17 = ",str17)
结果:
is a good
sunck
str17 = man!
str18 = "sunck is a good man"
print("good" in str18)
print("good1" not in str18)
结果:
True
True
#格式化输出
print("sunck is a good man ")
num = 10
str19 = "sunck is a nice man!"
f = 10.126
print("num =", num,"str19 = ",str19)
#%d %s %f 占位符
# 精确到小数点后3位,会四舍五入
print("num = %d,str19 = %s,f = %.3f"%(num,str19,f))
sunck is a good man
num = 10 str19 = sunck is a nice man!
num = 10,str19 = sunck is a nice man!,f = 10.126
#如果字符串内有很多换行,用\n写在一行不好阅读
print("good\nnice\nhandsome")
print('''
good
nice
handsome
''')
例如
创建一个字符串
name = 'hongwei'
print(type(name)) #type是获取类的
print(dir(name)) #dir可以列出类里面所有的成员(包括功能,方法等)
>>> dir(str)
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__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']
__contains__ 等于 in
name = 'hongwei'
result= name.__contains__('on') #是否包含on
# result = 'on' in name
print(result)
结果 True #结果True包含
#len(str)
#返回字符串的长度
print(len("sunck is a good man"))
结果:19
#lower()转换字符串中大写字母为小写字母
str20 = "SUNCK is a good man"
str21 = str20.lower()
print(str21)
print("srt20 = %s"%str20)
结果:
sunck is a good man
srt20 = SUNCK is a good man
#upper()转换字符串中小写字母为大写字母
str21 = "SUNCK IS A Good Man!"
print(str21.upper())
结果:SUNCK IS A GOOD MAN!
#swapcase()转换字符串中小写字母为大写字母,大写字母为小写字母
str22 = "SUNCK is a gOOd mAn"
print(str22.swapcase())
结果:sunck IS A GooD MaN
#character char
#center(width,fillchar)
#返回一个指定宽度的居中字符串,fillchar为填充的字符串,默认空格填充
str25 = "kaige is a nice man"
print(str25.center(40,"*"))
结果:**********kaige is a nice man***********
__getattribute__ 反射的时候会用到
__getitem__ 面向对象是用到
capitalize 首字母大写
name = 'eric'
result = name.capitalize()
print(result)
结果 Eric
casefold 首字母小写
center 添加空行符
name = 'Hongwei'
result = name.center(20,'*')
print(result)
结果 ******Hongwei*******
count 找子序列出现的次数
name = "asdfsjkldf;fdgrfdf"
result = name.count("df",0,4)
print(result)
encode 转换字节码
name = '李杰'
result = name .encode('gbk')
print(result)
endwith 判断字符是否在字符串中
name = 'hongwei'
result = name.endswith('n',0,3)
print(result)
name = "hongwei"
result = name.find("n")
#result = name.index('n')
print(result)
#find和index都是去字符串里找字符,find如果没找到会返回-1,index如果没找到会报错
format 做字符串格式化
name = "grand {0} as {1}"
result = name.format('handsome','eric')
print(result)
#name = "grand {name} as {id}"
#result = name.format(name = 'handsome',id = 'eric')
#print(result)
结果:grand handsome as eric
join 拼接
ll = ['g','r','a','n','d']
result = " ".join(ll)
print(result)
结果 grand