name1 = "六星" name2 = "教育" print(name1 + name2) print(name1,name2)
2、*重复输出
需要输出多少次*后面就写多少
print("六星教育"*3)
3、成员运算符
成员运算符的作用:检查字符串中是否包含某一个或者多个字符
3.1 in:如果包含的话,返回为True
name = 'sixStar'
print('s' in name)
print('e' in name)
3.2 not in:如果不包含的话,返回为True
name = "六星教育"
print("six" not in name)
print("六星" not in name)
4、下标/索引
Python的下标是从0开始的
语法格式:字符串名[下标值]
示例一:
name = "sixStar" print(name[0])
示例二:从右往左,下标是从-1开始的
name = "sixStar" print(name[-2])
5、切片
语法格式:[开始位置:结束位置:步长]
name = "sixstar" print(name[0:3]) #six print(name[2:5]) #xst #从右往左取 print(name[-2:-5]) # print(name[-5:-2]) #xst
步长:表示取间隔、不写步长,则默认为1
name = "sixstar" print(name[-2:-5:-1]) #ats -1代表从右往左切取
6、find
find作用:检测字符串是否包含在字符串中
name = "susu" print(name.find("s")) print(name.find("su"))
7、count
count作用:返回出现的次数,没有则返回0
name = "susu python" print(name.count('s')) print(name.count("n"))
8、replace
replace作用:替换
语法格式:replace(旧内容,新内容,替换次数)
name3 = "好好学习,天天向上" print(name3.replace('天','时',2))
st = 'hellopython' print(st.replace('h','a',2))
9、split
split作用:指的分隔符来切字符串
st = "hello python" print(st.split(' ')) #列表形式
10、index
index作用:跟find作用一样,但是会报错
name = "哈哈哈今天天气真不错哈" print(name.index("今")) print(name.index('哈',2))
find没找到返回-1,index没找到返回异常。
11、capitalize
capitalize作用:第一个字符大写
st = "sixstar" print(st.capitalize()) #其他都小写 st1 = "sIXSTAR" print(st1.capitalize())
12、startswith
st = "sixstar"
print(st.startswith("s"))
print(st.startswith("six"))
print(st.startswith("sex"))
13、endswith
endswith作用: 是否以某字符结束,是的话返回为True,不是的话返回为False
st = "sixstar" print(st.endswith("r")) print(st.endswith("ar")) print(st.endswith("s"))
14、 lower
lower作用: 大写字母转为小写
st = "SIXSTAR" print(st.lower())
15、upper
upper作用: 小写字母转为大写
st = "sixstar" print(st.upper())
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!