字符串的定义
单双引号的歧义:
a = 'westos'
b = "westos's"
c = 'what\'s'
输出一段文字:
d="""
用户管理系统
1.添加用户
2.删除用户
3.显示用户
"""
print(d)
字符串的特性
索引
s = 'hello'
# 索引:0 1 2 3 4 索引从0开始
print(s[0])
print(s[4])
print(s[-1]) # 拿出最后一个字符
结果
切片
s[start:stop:step] 从start开始到stop-1结束 step:步长
s = 'hello'
print(s[0:3])
print(s[0:4:2])
print(s[:]) #显示全部的字符
print(s[:3]) #显示前3个字符
print(s[::-1]) #字符串的反转
print(s[2:]) #除了前2个字符之外的其他字符
结果
重复
s = 'hello'
print(s * 10)
结果
连接
s = 'hello'
print('hello ' + 'python')
结果
成员操作符
s = 'hello'
print('he' in s)
print('aa' in s)
print('he' not in s)
结果
for循环遍历
s = 'hello'
for i in s:
print(i,end='')
结果