1.字符串的基本特性:
1-1: 字符串的输入:
单引号,双引号,三引号引起来的都是字符串 ;
转译字符:
\n 换行符
\t 制表符
1-2 : 连接和重复
连接操作符: + (只能同类型的数据类型拼接)
>>> name = "westos"
>>> print('hello ' + name)
hello westos
>>> print('hello ' + str(1))
hello 1
重复操作符: *
>>> print("*" * 30 + '学生管理系统' + '*' * 30)
******************************学生管理系统******************************
1-3: 成员操作符: in 返回bool值 ;
>>> s = 'hello westos'
>>> print('westos' in s)
True
>>> print('westos' not in s)
False
1-4: 正向索引和反向索引 :
索引:获取特定偏移的元素; 分为正向索引和反向索引 (表示只拿一个字符)
>>> s = 'WESTOS'
>>> print(s[0]) # W (正数第一个)
W
>>> print(s[3]) # T (正数第四个)
T
>>> print(s[-3]) # T (倒数第三个)
T
1-5: 切片 (拿多个字符):
s[start : end : step] 和range()类似 ,start索引开始,end-1索引结束,部长为step ;
s[:end] start没写默认从0 开始 ;
>>> s = "hello westos"
>>> print(s[1:3]) #从第一个索引开始,到3-1个索引结束 , # el ;
el
>>> print(s[:3]) #从头开始,到3-1个索引结束, # hel ;
hel
>>> print(s[1:]) #从第1个索引开始拿到最后 # ello westos
ello westos
>>> print(s[:]) #拷贝字符串
hello westos
常见切片表示的意义:
s[:n] 拿出前n个字符
s[n:] 拿出除了前n个元素
s[:] 从0个索引开始到结束
s[::-1] 倒序输出
2: 使用for循环遍历字符串:
>>> s = 'westos'
>>> count = 0
>>> for item in s:
... count += 1
... print(f"第{count}个字符{item}")
...
第1个字符w
第2个字符e
第3个字符s
第4个字符t
第5个字符o
第6个字符s
3.练习1:
需求:用户输入一个字符串,判断该字符串是否为回文字符串 (回文字符串:例如: aba , abba等);
法一:
s = input("请输入字符串:")
s1 = s