string

什么是字符串
以单引号或双引号括起来的任意文本 ‘abc’ “def”,字符串不可变
创建字符串

str1 = “good good study!”
str3 = “day day up!”

字符串运算

#字符串连接
str6 = "sunck is a "
str7 = "good man"
str8 = str6 + str7
print ("str8 = ", str8)

#输出重复字符串
str9 = "good"
str10 = str9 * 3
print(“str10 = ”, str10)

#访问字符串中的某一个字符,通过索引下标查找字符,索引从0开始
str11 = “sunck is a goog man!”
print(str11[1])
str11[1] = “a”	#!!!!!字符串不可变!!

#截取字符串的一部分
str13 = “sunck is a good man!”
str15 = str13[6:14]
#从头截取到给定下标之前
str16 = str13[:5]
#从给定下标处开始截取到结尾
str17 = str13[16:]
print(“str15 =”, str15)

#判断字符串中元素存在
str18 = “sunck is a good man”
print(“good” in str18)	#True/False

#能被4整除但不能被100整除或能被400整除
year = int(input())
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
	print(“是闰年”)
else:
	print(“不是闰年”)

#eval()
#功能:将字符串str当成有效的表达式来求值并返回计算结果
num1 = eval(“123”)
print(num1)
print(type(num1))
print(eval(“+123”))
print(eval(“12+3”))

#len(str)
#返回字符串的长度(字符个数)
print(len(“sun is a good man”))

#lower(str)转换字符串中的大写字母为小写字母
str20 = “SUNCK is a Good man”
print(str20.lower())
print(“str20 = %s” %(str20))

#upper()转换字符串中的小写字母为大写字母
print(“SUNCK is a Good man” . upper())

#swapcase()	转换字符串中的大小写字母
str22 = “SUNCK is a gOOd mAn”
print(str22.swapcase())

#capitalize()	首字母大写,其他小写
str23 = “SUNCK is a gOOd mAn”
print(str23.capitalize())

#title()	每个单词的首字母大写
str24 = “SUNCK is a gOOd mAn!”
print(str24.title())

#center(width[, fillchar])	返回一个指定宽度的字符串,fillchar为填充的字符
#默认空格填充
str25 = “kaige is a nice man”
print(str25.center(40, “*”))

#ljust(width[, fillchar])
#返回一个指定宽度的左对齐字符串
char26 = “kaige is a nice man”
print(str26.ljust(40))

#rjust(width[, fillchar])
#返回一个指定宽度的右对齐字符串

#zfill(width)	返回一个长度为width的字符串,右对齐,左边补0
str28 = “kaige is a nice man”
print(str28. zfill(40))

#count(str[, start][,end])
#返回字符串中str出现的次数,可以指定一个范围,默认从头到尾
str29 = “kaige is a very very nice man”
print(str29. count(“very”, 15, len(str29)))

#find(str[, start][, end])
#从左到右检测str字符串是否包含在字符串中,可以指定范围,得到的是第一次出现的开始下标,没有返回-1
str30 = “kaige is a very very nice man”
print(str30.find(“very”))

#rfind(str[, start][, end])
#从右到左检测str字符串是否包含在字符串中,可以指定范围,得到的是第一次出现的开始下标,没有返回-1
str30 = “kaige is a very very nice man”
print(str30.rfind(“very”))

#index(str, start = 0, end = len(str))
#跟find一样,如果str不存在,会报一个异常
str31 = “kaige is a very very nice man”
print(str31.index(“good”))

#rindex(str, start = 0, end = len(str))
#从右向左查找

#lstrip()	截掉字符串左侧指定的字符,默认为空格
str33 = “     ****good good study”
print(str33.lstrip(“*”))

#rstrip()	截掉字符串右侧指定的字符,默认为空格
str34 = “     ****good good study***       ”
print(str33.rstrip(“*”))

#strip()	
str35 = “*****kaige is a nice man*******8”
print(str35.strip(“*”))

#split(str = “”)
#以str为分隔符截取字符串,指定num,则仅截取num个字符串
str38 = "good is****a****study**up"
list39 = str38.split(“*”)
#print(str38.split(“*“, 3))

c = 0
for s in list39:		#单词个数
	if len(s) > 0
		c += 1
print(c)

#splitlines([keepends])	按照(‘\r’, ‘\r\n’, ‘\n’)分隔,
#keepends==True 保留换行符
str40 = '''good good study
good great study
good hard study
'''
print(str40.splitlines())

#join()		
#以指定的字符串分隔符,将seq中的所有元素组合成一个字符串
list41 = ['sunck', 'is', 'a', 'good', 'man']
str42 = " ".join(list41)
print(str42)

#max()		min()
str43 = “ good good study!z”
print(max(str43))
print(“*” + min(str43) + “*”)

#replace(oldstr, newstr, count)
#用newstr替换oldstr,默认全部替换,如果指定了count,只替换前count个
str44 = “good good study! good good study”
str45 = str44.replace(“good”, “nice”, 1)
print(str44)
print(str45)

#创建一个字符串映射表
#要转换的字符串目标字符串
t46 = str.maketrans(“ac”, “65”)	#a—6		c—5	
str47 = “sunck is a good good good man”
str48 = str47.translate(t46)
print(str48)

#startwith(str, start=0, end=len(str))
#在给定的范围内判断是否以给定的字符串开头,默认整个字符串
str49 = “sunck is a good man”
print(str49.startwith(“sun”, 5, 16))

#endwith()
str50 = “sunck is a good man”
print(str50.endwith(“man”))