1.4字符串操作

#字符串操作
str1 = 'Hello'
str2 = 'World'
str3 = '!'
str4 = str1 + ',' + str2 + str3
print(str4) # Hello,World!

str5 = str4 * 3
print(str5) # Hello,World!Hello,World!Hello,World!

#统计字符个数
print(len(str5)) # 36

str6 = 'abcdefghi'
print(str6[0]) # a
print(str5[-1])# !

#切片
#包括起始下标,不包括结束下标,默认步进值为1
print(str6[1:3]) # bc
print(str6[:4])  # abcd
print(str6[1:-2]) # bcdefg
print(str6[-3:]) # ghi
print(str6[:-2]) # abcdefg
print(str6[:]) # abcdefghi

#当步进不等于1时
print(str6[::2]) # acegi
print(str6[::-1]) # ihgfedcba 此时为逆序

猜你喜欢

转载自blog.csdn.net/xc_lmh/article/details/81146444