东莞企业网站建设费用,有关网站建设的标题,深圳旅游网站建设,梅州免费建站找哪家先看一下本篇文章要讲的内容目录#xff1a;
4.2 字符串入门String4.2.1 repr和字符串4.2.2 input和raw_input4.2.3 长字符串4.2.4 bytes4.2.5 字符串格式化4.2.6 Python自带两个帮助函数4.2.7 删除多余空白4.2.8 字符串的查找#xff0c;替换4.2.9 字符串的分割#xff0c;…先看一下本篇文章要讲的内容目录
4.2 字符串入门String4.2.1 repr和字符串4.2.2 input和raw_input4.2.3 长字符串4.2.4 bytes4.2.5 字符串格式化4.2.6 Python自带两个帮助函数4.2.7 删除多余空白4.2.8 字符串的查找替换4.2.9 字符串的分割连接方法4.2.9 运算符---------------------
4.2 字符串入门String
字符串的意思就是“一串字符”比如“Hello田心木瓜”是一个字符串。Python中的字符串用单引号 ’ 或双引号 括起来同时使用反斜杠 \ 转义特殊字符(比如对引号进行转义)。也可以在字符串前面添加一个 r表示原始字符串。
【如下代码全部在Ipython实现后续如无特殊说明都指在Ipython下】
In [1]: txmg It is a \charcter\. #转义字符的使用In [2]: print (txmg)It is a charcter.字符串的截取的语法格式如下
变量[头下标:尾下标]
索引值以 0 为开始值-1 为从末尾的开始位置。
In [3]: ss ABCDEFGHIJ #定义一个字符串In [7]: ss[2]Out[7]: CIn [8]: ss[0]Out[8]: AIn [9]: ss[-9]Out[9]: BIn [10]: ss[1]Out[10]: BIn [11]: ss[-1]Out[11]: J加号 是字符串的连接符 星号 * 表示复制当前字符串紧跟的数字为复制的次数。实例如下
In [13]: print (ss[0:-1]) #第一个到最后不包含最后一个JABCDEFGHIIn [14]: print (ss[1:5]) # 输出从第二个开始到第五个的字符BCDEIn [15]: print (ss[3:]) #输出从第4个开始到最后DEFGHIJIn [16]: print (ss * 3) # 输出字符串3次ABCDEFGHIJABCDEFGHIJABCDEFGHIJIn [17]: print (ss hhhhh) # 连接字符串ABCDEFGHIJhhhhh小结
反斜杠可以用来转义使用r可以让反斜杠不发生转义。字符串可以用运算符连接在一起用*运算符重复。Python中的字符串有两种索引方式从左往右以0开始从右往左以-1开始。Python中的字符串不能改变。4.2.1 repr和字符串
Python不允许直接拼接数值和字符串必须先将数值转换成字符串。
可以使用str()或repr()函数举例如下
In [18]: meat 猪肉很贵啊价钱是:In [19]: price 88.78In [20]: print (meat price)---------------------------------------------------------------------------TypeError Traceback (most recent call last) in ---- 1 print (meat price)TypeError: can only concatenate str (not float) to strIn [21]: print (meat str(price))猪肉很贵啊价钱是:88.78In [22]: print (meat repr(price))猪肉很贵啊价钱是:88.784.2.2 input和raw_input
input()函数用于向用户生成一个提示然后获取用户输入的内容此函数总会将用户输入的内容放入字符串中因此用户输入任何内容input()函数总是返回一个字符串。
raw_input()是python 2中的相当于python 3中的inputIn [27]: i input()1In [28]: print (type(i))In [29]: j input()2.89In [30]: print (type(j))In [31]: h input()helloIn [32]: print (type(h))4.2.3 长字符串
‘’三个引号一般用于多行注释但是也可以将它赋值如下例子。
In [33]: ls It is a long long long....: stroy ,please read it....: are u ok?...: In [34]: print (ls)It is a long long long.stroy ,please read it.are u ok?4.2.4 bytes
python3 新增bytes类型str是以多个字符组成 bytes是以多个字节组成bytes只负责以字节二进制格式序列来记录数据由于bytes保存原始的字节二进制格式数据因此bytes对象可用于网络上传输数据也可用于存储各种二进制格式的文件。比如图片音乐等文件。
如果字符串内容都是ASCII字符则可直接在字符串之前添加b来构建bytes值。
调用bytes()函数将字符串按照指定字符集转换成bytes默认使用UTF-8字符集。
调用字符串本身的encode() 方法将字符串按指定字符集转换成bytes默认使用UTF-8字符集4.2.5 字符串格式化
Python提供了“%”对各种类型的数据进行格式化输出In [35]: number -29In [36]: print (number is %6i % number)number is -29In [37]: print (number is %6d % number)number is -29In [38]: print (number is %6o % number)number is -35In [39]: print (number is %6x % number)number is -1dIn [40]: print (number is %6X % number)number is -1DIn [41]: print (number is %6S % number)In [42]: print (number is %6s % number)number is -29%6 - 6是指输出最小宽度为6。转换浮点数的宽度可以用%6.3f感兴趣可以自己试一下
python中字符串还支持用in运算符判断是否包含某个子串还有获取字符串的长度用内置len()函数还可以用min()和max()函数获取字符串中最小字符和最大字符
In [43]: ah This is has two dogs.In [44]: two in ahOut[44]: TrueIn [48]: nn in ahOut[48]: FalseIn [49]: len(ah)Out[49]: 21In [50]: min(ah)Out[50]: In [51]: max(ah)Out[51]: w4.2.6 Python自带两个帮助函数
dir():列出指定类或模块包含的全部内容包括函数方法类变量等
help():查看某个函数或方法的帮助文档
感兴趣的可自行尝试 比如dir(str)
4.2.7 删除多余空白
字符串还提供了了如下常用的方法来删除空白
strip()删除字符串前后的空白
lstrip()删除字符串前面左边的空白
rstrip()删除字符串后面右边的空白
注意因为字符串类型是不可变的所以上述三个方法是删除空白的副本并没有真正改变字符串本身
In [55]: abc the is hh In [56]: print (abc)the is hhIn [57]: abc.strip()Out[57]: the is hhIn [58]: abc.lstrip()Out[58]: the is hh In [59]: abc.rstrip()Out[59]: the is hhIn [60]: print (abc)the is hh还可以删除指定字符的In [80]: s123 the is hhhhhhiooIn [81]: print (s123)the is hhhhhhiooIn [82]: print (s123.lstrip(th))e is hhhhhhiooIn [83]: print (s123.rstrip(isioo))the is hhhhhhIn [84]: print (s123.strip(tioo))he is hhhhhh4.2.8 字符串的查找替换
startswith(): 判断字符串是否以指定子串开头。
endstwith():判断字符串是否以指定子串结尾。
find():查找指定子串在字符串中出现的位置如果没有知道指定子串则返回-1.
index():查找指定子串在字符串中出现的位置如果没有知道指定子串则引发VauleError错误
replace():使用指定子串替换字符串中的目标子串。
translate():使用指定的翻译映射表对字符串执行替换。
4.2.9 字符串的分割连接方法
split(): 将字符串按指定分隔符分割成多个短语join(): 将多个短语连接成字符串
In [87]: print (a1.split()) #使用空白对字符串进行分割[crazyPython, is, a, good, book]In [88]: print (a1.split(None, 2)) #使用空白对字符串进行分割最多只分割前两个单词[crazyPython, is, a good book]In [91]: print (a1.split(y)) #使用y来进行分割[craz, P, thon is a good book]In [92]: my_a1 a1.split()In [94]: print (my_a1)[crazyPython, is, a, good, book]In [95]: print (/.join(my_a1))crazyPython/is/a/good/bookIn [96]: print (,.join(my_a1))crazyPython,is,a,good,book4.2.9 运算符
python运算符
赋值运算符算术运算符位运算符索引运算符In [97]: a10 abcdefghijklmnopqIn [99]: print (a10[2:8:3]) #获取索引2到索引8的子串步长为3cfIn [100]: print (a10[2:8:2]) #获取索引2到索引8的子串步长为2ceg比较运算符逻辑运算符三目运算符
In [101]: bb 12In [102]: cc 4In [103]: st bb 大于 cc if bb cc else bb不大于ccIn [104]: print (st)bb 大于 cc未完待续