以下所有的实操全在redhat7.3真机上pycharm
字符串定义
字符串是 Python 中最常用的数据类型。我们可以使用引号( ’ 或 " )来创建字符串。
创建字符串很简单,只要为变量分配一个值即可。例如:
1
2
3
4
5
6
7
8
9
10
11a='westos' b="zhao" c=""" 用户管理系统 添加用户 删除用户 """ print(a) print(b) print(c)
字符串的特性
Python 访问字符串中的值
Python 不支持单字符类型,单字符在 Python 中也是作为一个字符串使用。
Python 访问子字符串,可以使用方括号来截取字符串,如下实例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
231.# s='helmo' #索引:0 1 2 3 4 索引从0开始 # print(s[0]) #截取第1个字符 这个s[0]类似与c语言中的数组下标, # # 而注释中所说的1是指字符串中的第一个字母 # print(s[4]) #拿出最后一个字符 # print(s[-1]) #拿出最后一个字符 可以写负数,这个也是python的特点 2.#s[start:stop:step] 从start 开始到end-1结束,步长为step # print(s[0:3]) #截取前3个字符前 0~3-1个字符 # print(s[0:4:2]) #步长为2,截取第1个和第3个字符 # print(s[:]) #截取全部 #显示前2个字符 # print(s[:2]) #截取前2个字符 #字符串的反转 # print(s[::-1]) #截取除了前2个字符以外的所有字符 # print(s[2:]) #重复输出 # print(s*5) #输出5遍
Python 字符串更新
你可以截取字符串的一部分并与其他字段拼接,如下实例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15s = 'hello world' #索引:0 1 2 3 4 5 6 7 8 9 10 索引从0开始 #拼接 print('已更新字符:', s[6:] + ' wang'+'yi'+'bo ' + s[:6]) #将除了前6个字符以外的+wangyibo+前6个字符的拼接 print('wang'+' yi '+'bo') #成员操作符 print('hell' in s) #'hell'字符是否存在与s字符串中 print('hl' in s) #同上 print('he' not in s) #'he'字符不存在于s字符串中 #for循环遍历 for i in s: ##循环将字符一个一个输出来 print(i)
Python转义字符
在需要在字符中使用特殊字符时,python用反斜杠()转义字符。如下表:
Python字符串运算符
下表实例变量a值为字符串 “Hello”,b变量值为 “Python”:
Python字符串格式化
Python 支持格式化字符串的输出 。尽管这样可能会用到非常复杂的表达式,但最基本的用法是将一个值插入到一个有字符串格式符 %s 的字符串中。
在 Python 中,字符串格式化使用与 C 中 sprintf 函数一样的语法。
python字符串格式化符号:
格式化操作符辅助指令:
Python三引号
python三引号允许一个字符串跨多行,字符串中可以包含换行符、制表符以及其他特殊字符。实例如下:
三引号让程序员从引号和特殊字符串的泥潭里面解脱出来,自始至终保持一小块字符串的格式是所谓的WYSIWYG(所见即所得)格式的。
一个典型的用例是,当你需要一块HTML或者SQL时,这时用字符串组合,特殊字符串转义将会非常的繁琐。
Python 的字符串内建函数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37在shell里面测试: 注意:去除左右两边的空格,空格为广义的空格 包括:t n [root@foundation10 ~]# python3 Python 3.6.4 (default, Aug 26 2019, 22:17:35) [GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> 'zhao'.istitle() #判断是否是标题 False >>> 'Zhao'.istitle() True >>> 'zhao'.isupper() #是否为大写 False >>> 'ZhaO'.isupper() False >>> 'ZHAO'.isupper() True >>> 'ZHAo'.islower() #是否为小写 False >>> 'zhao'.islower() True >>> a = 'hello'.lower() #将其符值给变量,再将其判决 >>> a 'hello' >>> a = 'Hello'.lower() >>> a 'hello' >>> b='HellO'.lower() #lower()方法将字符串中的大写字母转为小写字母 >>> b 'hello' >>> b='hello'.upper() #upper() 方法将字符串中的小写字母转为大写字母。 >>> b 'HELLO' >>> a = 'HELLO'.title() # title() 方法返回"标题化"的字符串,就是说所有单词都是以大写开始,其余字母均为小写(见 istitle())。 >>> a 'Hello'
字符串中的endswith()和startswith()两个函数的用法
startswith()函数:
描述:
Python startswith() 方法用于检查字符串是否是以指定子字符串开头。语法
startswith()方法语法:
str.startswith(str, beg=0,end=len(string));
参数
str – 检测的字符串。
strbeg – 可选参数用于设置字符串检测的起始位置。
strend – 可选参数用于设置字符串检测的结束位置。
返回值
如果检测到字符串则返回True,否则返回False。
endswith()函数:
描述
Python endswith() 方法用于判断字符串是否以指定后缀结尾。用法:
endswith()方法语法:
str.endswith(suffix[, start[, end]])
参数
suffix – 该参数可以是一个字符串或者是一个元素。
start – 字符串中的开始位置。
end – 字符中结束位置。
返回值
如果字符串含有指定的后缀返回True,否则返回False。
例子:
1
2
3
4
5
6
7
8
9
10
11
12filename = 'hello.loggg' if filename.endswith('.loggg'): print(filename) else: print('error.file') url = 'https://172.25.254.9/index.html' if url.startswith('https://'): print('爬取内容') else: print('不能爬取')
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30在shell上操作测试的: >>> >>> s = ' hello' #空格输出一个table键就会出现t >>> s 'thello' #脏数据 >>> s = ' hello' >>> s ' hello' >>> s.lstrip() 'hello' >>> s = ' hello ' >>> s ' hello ' >>> s.lstrip() 'hello ' >>> s.rstrip() ' hello' >>> s.strip() 'hello' >>> s=' hellon' >>> s ' hellon' >>> s = 'helloh' >>> s.strip('h') #去除两边的h 'ello' >>> s.lstrip('h')#去除左边的h 'elloh' >>> s.strip(' h') #括号里写去掉什么写什么; 'eell'
字符串的判断
isalpha() 方法检测字符串是否只由字母组成
isdigit() 方法检测字符串是否只由数字组成
isalnum() 方法检测字符串是否由字母和数字组成
变量名的定义
变量名定义是否合法:
1.变量名可以由字母、数字、下划线组成
2.变量名只能以字母或者下划线开头
练习:
1.判断字符串是否合法
2.当字符串中有特殊字符如何排除;例如:s =‘sy%%%’
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16while True: s = input('变量名:') if s == 'exit': print('exit') break if s[0].isalpha() or s[0] == '_': for i in s[1:]: if not (i.isalnum()or i == '_'): print('%s变量名不合法' %(s)) break else: print('%s变量名合法' %(s)) else: print('%s变量名不合法' % (s))
字符串对齐:
print(‘公司财务管理系统’.center(10)) #字符串从左边向右总共10空格
print(‘公司财务管理系统’.center(30,’$’))#将其“学生管理系统”放在中间总共长度30
print(‘公司财务管理系统’.ljust(30,’@’)) #注意特殊字符只可以写一个,不然多数会报错
print(‘公司财务管理系统’.rjust(30,’%’))
字符串的替换
1
2
3
4
5
6
7
8s = 'hello world zhao hello' #find找到字符串,并返回最小的索引 print(s.find('hello')) print(s.find('world')) print(s.rfind('hello')) #从右边查找; #替换字符串的hello为westos print(s.replace('hello','westos'))
字符串的统计
print(‘hello’.count(‘l’))
print(‘hello’.count(‘ll’))#统计字符串中的字符个数
print(‘hello’.count(‘m’))
print(len(‘wangyibo’)) #统计字符串长度
字符串的分离与连接
1
2
3
4
5s = '172.25.254.10' s1 = s.split('.') #以”.“的字符将其s分离开 print(s1) #打印出来是一个列表 print(s1[::-1]) #反向输出s
1
2
3
4
5#连接 通过指定的连接符号 连接每个字符 print(''.join(s1)) print('/'.join(s1)) print('_'.join('westos'))
练习:
1、设计一个程序,帮助小学生练习10以内的加法:
详情:
1.随机生成加法题目;
2.学生查看题目并输入答案;
3.判别学生题目是否正确?
4.退出时,统计学生答题总数,正确数量及正确率(保留两位小数点);
2、小学生算术能力测试系统:
设计一个程序,用来实现帮助百以内的算术练习,它具有以下功能:提供10道加减乘除四种基本算术运算的题目;
练习者根据显示的题目输入自己的答案,
程序自己判断输入的答案是否正确并显示出相应的信息;
最后
以上就是个性纸飞机最近收集整理的关于Python 字符串的全部内容,更多相关Python内容请搜索靠谱客的其他文章。
发表评论 取消回复