在python中字符串的一些用法与数组很相似,他们都具有一些相同的函数可以调用,对于字符串的这些方法首先要了解其用法,因为字符串在未来的学习中会多次使用,我们也要了解其特点,方便后续的学习!!!
str_1="Forever is handsome!"
#下标索引,正向和反向
print(str_1[2])
print(str_1[-1])
#index(下标索引)
value=str_1.index("is")
print(f"‘is’ position is {value}")
#replace(str_1,str_2) (替换)
str_2=str_1.replace("Forever","刘")
print(str_2)
#split(分离)
str_3=str_1.split(" ")
print(str_3)
str_4=str_1.split("i")
print(str_4)
#strip
my_str=" Forever "
new_my_str=my_str.strip() #去头、尾的空格
print(new_my_str)
my_str1="12Forever21" #去掉字符串1和字符串2
new_my_str_1=my_str1.strip("12")
print(new_my_str_1)
#count
str_2="Forever"
count=str_2.count("e")
print(count)
#len
str_3="I'm beautiful!"
print(len(str_3))