PR4E第六周笔记

来源:互联网 发布:aria2 百度网盘windows 编辑:程序博客网 时间:2024/05/29 09:38
str[0:5] 截取第一位到第四位的字符
str[:] 截取字符串的全部字符
str[4:] 截取第五个字符到结尾
str[:-3] 截取从头开始到倒数第三个字符之前
str[2] 截取第三个字符

str[::-1] 创造一个与原字符串顺序相反的字符串

str代表对其操作的字符串,而后的是方括号而非普通括号,里面的数字代表字符串中字符的序号,序号从0开始,第n个字符在n-1位。

字符串函数使用方法为输入str.fun(),其中str代表进行操作的字符串,fun()代表使用的函数,两者间用.连接。

find(,)用来在字符串中寻找一个字符,返回找到的字符序号,逗号后是开始搜索的字符序号。

 upper()  lower()对字符字母进行大小写转换

replace(old,new)对字符串中进行字符替换

 dir()列出对当前字符串可使用的函数 

rstrip() lstrip() srip() 清除字符串左边或者右边或者两边的空白字符。

这次的作业是

6.5 Write code using find() and string slicing (see section 6.10) to extract the number at the end of the line below. Convert the extracted value to a floating point number and print it out.

完成的结果是

text = "X-DSPAM-Confidence:    0.8475"space = text.find(" ")raw_num = text[space:]num = raw_num.strip()num = float(num)print num




0 0
原创粉丝点击