字符串定义:是以单引号或双引号括起来的任意文本 'abc' "abc"(字符串不可变)
创建字符串
str1 = "sunck is a good man!"
str3 = "sunck is a nice man!"
str5 = "sunck is a handsome man!"
print(str1)
print(str3)
print(str5)
str6 = "sunck is a "
str7 = "good man"
str8 = str6 + str7
print(str6)
print(str7)
print(str8)
str9 = "good"
str10 = str9 * 3
print(str10)
str11 = "this is a good man!"
for i in range(len(str11)):
print(str11[i],end = '\t')
str13 = "this is a good man!"
str15 = str13[8:15]
str16 = str13[:5]
print(str15)
print(str16)
str18 = "this is a good man!"
print("good" in str18)
print("good1" in str18)
- %d %s %f 占位符
- %-8d 左对齐,占8位
- %8d 右对齐,占8位
- %.4f小数点后面精确4位,支持四舍五入
num = 10
str19 = "this is a good man!"
f = 10.1234789
print("num = ",num,"str19 = ",str19)
print("num = %8d\nstr19 = %s\nf = %.4f" %(num, str19, f)
str19 = "this is a good man!"
print(len(str19))
str20 = "SUNCK is a good Man"
print(str20.lower())
print(str20)
str20 = "SUNCK is a good Man"
print(str20.upper())
print(str20)
str22 = "SUNCK is a gOOd mAn"
print(str22.swapcase())
print(str22)
str23 = "SUNCK is a gOOd mAn"
print(str23.capitalize())
print(str23)
str23 = "SUNCK is a gOOd mAn"
print(str23.title())
print(str23)
str25 = "kaige is a good man"
print(str25.center(40,"*"))
print(str25)
str25 = "kaige is a good man"
print(str25.ljust(40))
print(str25.ljust(40,"*"))
print(str25)
str25 = "kaige is a good man"
print(str25.rjust(40))
print(str25.rjust(40,"*"))
print(str25)
str28 = "kaige is a good man"
print(str28.zfill(40))
print(str28)
str29 = "kaige is a good man"
print(str29.count("very"))
print(str29.count("good",1,len(str29)))
print(str29)
count(str[,start][.end])
- 从左向右检测str字符串是否包含在字符串中,可以指定范围
- 默认是从头到尾,得到第一次出现的下标,没有返回-1
str30 = "kaige is very very very a good man"
print(str30.find("very"))
print(str30.find("good"))
print(str30.find("very",15,len(str30)))
- 从右向左检测str字符串是否包含在字符串中,可以指定范围
- 默认是从尾到头,得到第一次出现的下标,没有返回-1
str30 = "kaige is very very very a good man"
print(str30.rfind("very"))
print(str30.rfind("good"))
print(str30.rfind("very",0,15))
- fing()一样,只不过如果str不存在的时候会报一个异常
str31 = "kaige is very very very a good man"
print(str31.index("good"))
- rstrip()截掉字符串左侧指定的字符,默认是空格
tr33 = "****************kaige is very a good man*******************"
print(str33.lstrip("*"))
print(str33.rstrip("*"))
print(str33.strip("*"))
- 不乱于心,不困于情。
- 不畏将来,不念过往。
- 如此,安好。