6–6. 字符串.创建一个 string.strip()的替代函数:接受一个字符串,去掉它前面和后面的 空格(如果使用 string.*strip()函数那本练习就没有意义了)

 'Take a string and remove all leading and trailing whitespace'

 def newStrip(str):
'delete blanks around a string'
_end = len(str)
_start = 0 # delete the blanks at the beginning of the string
for i in range(_end):
if str[i] != ' ':
_start = i
break
else:
print 'invalid: The string is a blank string.' # if the string is a
_blank = True # blank string # delete the blanks in the end of the string
for i in range(-1, _start - _end, -1):
if str[i] != ' ':
_end = _end + i
break
if not _blank:
print '-' + str[_start: _end] + '-' # print '-' make sure the function work # test
if __name__ == '__main__':
newStrip(raw_input('Enter a string: '))

6–7. 调试.看一下在例 6.5 中给出的代码(buggy.py)

(a)研究这段代码并

python 核心编程第六章课后题自己做的答案的更多相关文章

  1. python核心编程第4章课后题答案(第二版75页)

    4-1Python objects All Python objects have three attributes:type,ID,and value. All are readonly with ...

  2. python核心编程第5章课后题答案

    5-8Geometry import math def sqcube(): s = float(raw_input('enter length of one side: ')) print 'the ...

  3. python核心编程第3章课后题答案(第二版55页)

    3-4Statements Ues ; 3-5Statements Use\(unless part of a comma-separated sequence in which case \ is ...

  4. python核心编程第2章课后题答案(第二版36页)

    2-5 Loops and Numbers a) i = 0    while i <11:     print i    i += 1 b) for i in range(0,11): pri ...

  5. python核心编程第六章练习6-8

    6-8.列表.给出一个整型值,返回代表该值得英文,比如输入89会返回“eight-nine”.附加题:能够返回符合英文语法规律的新式,比如输入89会返回“eighty-nine”.本练习中的值假定在0 ...

  6. python核心编程第六章练习6-14

    随机数.设计一个“石头.剪子.布”游戏,有时又叫“Rochambeau”,你小时候可能玩过,下面是规则.你和你的对手,在同一时间做出特定的手势,必须是下面一种:石头.剪子.布.胜利者从下面的规则产生, ...

  7. python核心编程第六章练习6-13

    6-13.字符串.string模块包含三个函数,atoi(),atol()和atof(),他们分别负责把字符串转换成整型.长整型和浮点型数字.从Python 1.5起,Python的内建函数int() ...

  8. python核心编程第六章练习6-11

    6-11.转换.(a)创建一个从整型到IP地址的转换,如下格式:www.xxx.yyy.zzz.(b)更新你的程序,使之可以逆转换.[答案](a)代码如下: Input_number = abs(in ...

  9. python核心编程第六章练习6-10

    6-10.字符串.写一个函数,返回一个跟输入字符串相似的字符串,要求字符串的大小写反转,比如,输入“Mr.Ed”,应该返回“mR.eD”作为输出.[答案]代码如下: #!/usr/bin/env py ...

随机推荐

  1. Truck History--poj1789

    Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 21534   Accepted: 8379 De ...

  2. excel筛选两列值是否相同,如果相同返回第三列值

    见图:

  3. ListView 实现分组

    1:FragmentHack4.java /** * Created by y on 15-1-2. */ public class FragmentHack4 extends Fragment{ V ...

  4. QDataStream类参考(串行化数据,可设置低位高位,以及版本号),还有一个例子

    QDataStream类提供了二进制数据到QIODevice的串行化. #include 所 有成员函数的列表. 公有成员 QDataStream () QDataStream ( QIODevice ...

  5. Unix,windows和Mac中的换行

    Unix 系统里,每行结尾只有“<换行>”,即“\n”:Windows系统里面,每行结尾是“<换行><回车 >”,即“\r\n”:Mac系统里,每行结尾是“< ...

  6. mybatis常用操作

    一.增 1.1 单条 <insert id="addUser" parameterType="com.xxx.model.UserInfo" useGen ...

  7. HMVC

    HMVC(Hierarchical-Model-View-Controller),也可以叫做 Layered MVC.顾名思义,就是按等级划分的 MVC 模式,简单的解释就是把MVC又细分成了多个子 ...

  8. linux操作系下RAR的使用

    ============zip文件的操作================================== zip -r data.zip data 解释:将data文件夹压缩成了data.zip格 ...

  9. lazy load 图片延迟加载 跟随滚动条

    http://plugins.jquery.com/lazyload/ Jquery.LazyLoad.js插件参数详解: 1,用图片提前占位 placeholder : "img/grey ...

  10. UVa10340.All in All

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...