1、字符串的定义方式
字符串的转义字符:\n换行;\t一个tab键
a = 'hello world'
b = 'what\'s up' ##在单引号中一些特殊字符需要转义
c = "what's up" ##双引号就不用转义
d = """
用户管理系统
1.添加用户
2.删除用户
3.显示用户
"""
print(a)
print(b)
print(c)
print(d)
## 运行结果
hello world
what's up
what's up
用户管理系统
1.添加用户
2.删除用户
3.显示用户
2、字符串的特性
字符串的特性包括:索引、切片、重复、成员操作符、迭代
- 索引: 0、1、2、3、4…… 索引值默认从0开始
s = 'hello'
print(s[1])
print(s[0])
print(s[-1])
运行结果
e
h
o
- 切片:s[start:end:step] 从start开始,到end-1结束,步长:step
print(s[0:3])
print(s[0:4:2])
#显示所有字符
print(s[:])
#显示前3个字符
print(s[:3])
#字符串逆序输出
print(s[::-1])
#除了第一个字符以外,其他全部输出
print(s[1:])
运行结果
hel
hl
hello
hel
olleh
ello
- 重复
print( s * 10)
运行结果
hellohellohellohellohellohellohellohellohellohello
- 连接
print(s + ' ' + 'linux' + ' ' + 'python')
运行结果
hello linux python
- 成员操作符
print('h' in s)
print('q' in s)
运行结果
True
False
- for循环(迭代)
for i in s:
print(i)
运行结果
h
e
l
l
o
- 练习:判断是否为回文数
"""
# _*_coding:utf-8 _*_
Name:字符串_02.py
Date:3/29/19
Author:LiMin-wsp
Connect:314690259@qq.com
Desc:
"""
"""
示例 1:
输入: 121
输出: true
示例 2:
输入: -121
输出: false
解释: 从左向右读, 为 -121 。 从右向左读, 为 121- 。因>此它不是一个回文数。
示例 3:
输入: 10
输出: false
解释: 从右向左读, 为 01 。因此它不是一个回文数。
"""
num = input('请输入数字: ')
if num == num[::-1]:
print('这是一个回文数')
else:
print('这不是回文数')
运行结果:
请输入数字: 12
这不是回文数
请输入数字: 1221
这是一个回文数
3、字符串的判断
判断字符串里每个元素是否为某种类型
一旦有一个元素不满足,就返回False
print('123'.isdigit()) ##判断是否为数字
print('123abc'.isdigit())
运行结果
True
False
print('Hello'.istitle()) ##判断是否为标题
print('hello'.istitle()) ##标题的第一个字母大写,其他的小写
运行结果:
True
False
print('hello'.upper()) ##将字符串变成大写
print('hello'.isupper()) ###判断字符串是否全是大写字母
print('hEllo'.lower()) ##将字符串变成小写
print('hEllo'.islower()) ##判断字符串是否全是小写
运行结果:
HELLO
False
hello
False
print('123aa'.isalpha()) ##判断字符串是否为纯字母
print('abdsa'.isalpha())
print('123ee'.isalnum()) ##判断字符串是否为数字字母
运行结果:
False
True
True
print(isinstance(1,int)) ##判断1 是否为int型
print(isinstance('a',str)) ##判断a是否为字符串型
print(isinstance(1,str))
运行结果
True
True
False
4、字符串去掉开头结尾空格
s = ' hello '
print(s)
print(s.strip()) ##去掉两边的空格
print(s.rstrip()) ##去掉右边的空格
print(s.lstrip()) ##去掉左边的空格
运行结果
hello
hello
hello
hello
s = '\nhello\t\t world' ##在字符串前面空出一行,hello后面空出两个tab键
print(s)
运行结果
hello world
s = 'hellohh'
print(s.strip('h')) ##将字符串去掉 h
print(s.rstrip('h')) ##去掉右边的h
print(s.lstrip('he')) ##去掉左边的he
运行结果
ello
hello
llohh
5、字符串匹配开头和结尾
filename = 'hello.log'
if filename.endswith('.log'): ##匹配以.log结尾的文件名
print(filename)
else:
print('error')
运行结果:
hello.log
url1 = 'file:///mnt'
url2 = 'ftp://172.25.254.250/pub'
url3 = 'http://172.25.254.250/index.html'
if url3.startswith('http://'): ##匹配以http://开头的网址
print('爬取该网页')
else:
print('错误网页')
运行结果:
爬虫该网页
6、字符串的搜索和替换
s = 'hello world hello'
print(s.find('hello')) #find找到子串,并返回最小的索引
print(s.find('world'))
print(s.rfind('hello')) #rfind找到子串,并返回最大的索引值(从右数最远的匹配的字符串的索引值)
print(s.replace('hello','westos')) # 替换字符串中所有的‘hello’为‘westos’
运行结果:
0
6
12
westos world westos
7、字符串的对齐
print('学生管理系统'.center(30)) ##在30个单位内居中
print('学生管理系统'.center(30,'*')) ##在30个单位内居中,两边用*填满
print('学生管理系统'.ljust(30,'*')) ##在30个单位内,居左并用*填满
print('学生管理系统'.rjust(30,'*')) ##在30个单位内,居右并用*填满
运行结果:
学生管理系统
************学生管理系统************
学生管理系统************************
************************学生管理系统
8、字符串的统计
print('heelleo'.count('e')) ##统计字符串中e的个数
print('heelleo'.count('ee')) ##统计字符串中ee的个数
print(len('hello')) ##统计字符串的长度
运行结果:
3
1
5
9、字符串的分离与连接
s = '172.25.25.60'
sl = s.split('.') ## 以点为标志符将字符串分离
print(s)
print(sl)
print(sl[::-1]) ##将分离开的字符串逆序输出
运行结果:
172.25.25.60
['172', '25', '25', '60']
['60', '25', '25', '172']
date = '2019-01-15'
date1 = date.split('-')
print(date1)
print(''.join(date1)) #通过指定的字符进行连接
print('/'.join(date1))
运行结果:
['2019', '01', '15']
20190115
2019/01/15
10、字符串综合练习
- 例1:判断变量名是否符合规则
while 1:
s = input('请输入变量名称: ')
if s == 'exit':
print('欢迎下次使用')
break
if s[0].isalpha() or s[0]=='_':
for i in s[1:]:
if not(i.isalnum() or i == '_'):
print('该变量名不合法')
exit()
else:
print('该变量名合法')
else:
print('该变量名不合法')
break
运行结果:
请输入变量名称: 123
该变量名不合法
请输入变量名称: qq123
该变量名合法
请输入变量名称: _123
该变量名合法
请输入变量名称: _123[]
该变量名不合法
- 例2:小米测试题
"""
# _*_coding:utf-8 _*_
Name:字符串_test_02.py
Date:3/29/19
Author:LiMin-wsp
Connect:314690259@qq.com
Desc:
"""
"""
(2017-小米-句子反转)
- 题目描述:
> 给定一个句子(只包含字母和空格), 将句子中的单词位置反转,>单词用空格分割, 单词之间只有一个空格,前>后没有空格。
比如: (1) “hello xiao mi”-> “mi xiao hello”
- 输入描述:输入数据有多组,每组占一行,包含一个句子(句子长度小于1000个>字符)
- 输出描述:对于每个测试示例,要求输出句子中单词反转后形成的句子
- 示例1:
- 输入:hello xiao mi
- 输出:mi xiao hello
"""
s = input('输入: ')
s1 = s.split(' ')
s2 = s1[::-1]
print(' '.join(s2))
运行结果:
输入: hello xiao mi
mi xiao hello
另:
print(' '.join(input('输入: ').split(' ')[::-1]))
- 例3:考勤记录
"""
# _*_coding:utf-8 _*_
Name:字符串_test_03.py
Date:3/29/19
Author:LiMin-wsp
Connect:314690259@qq.com
Desc:
"""
"""
给定一个字符串来代表一个学生的出勤纪录,这个纪录仅包含以下三个
字符:
'A' : Absent,缺勤
'L' : Late,迟到
'P' : Present,到场
如果一个学生的出勤纪录中不超过一个'A'(缺勤)并且不超过两个连续的'L'(迟到),
那么这个学生会被奖赏。
你需要根据这个学生的出勤纪录判断他是否会被奖赏。
示例 1:
输入: "PPALLP"
输出: True
示例 2:
输入: "PPALLL"
输出: False
"""
s = input('输入: ')
if s.count('A')<=1 and s.count('LLL')<=0:
print('True')
else:
print('False')
运行结果:
输入: PPALLL
False
输入: PPALLP
True
- 例4:字符串剪辑
"""
# _*_coding:utf-8 _*_
Name:字符串_test_04.py
Date:3/29/19
Author:LiMin-wsp
Connect:314690259@qq.com
Desc:
"""
"""
- 题目描述:
输入两个字符串,从第一字符串中删除第二个字符串中所有的字符。例
如,输入”They are students.”和”aeiou”,
则删除之后的第一个字符串变成”Thy r stdnts.”
- 输入描述:
每个测试输入包含2个字符串
- 输出描述:
输出删除后的字符串
- 示例1:
输入
They are students.
aeiou
输出
Thy r stdnts.
"""
s1 = input('输入1: ')
s2 = input('输入2: ')
for i in s2:
s1=s1.replace(i, '')
print(s1)
运行结果:
输入1: they are student
输入2: aeiou
thy r stdnt