6-1 字符串 .string 模块中是否有一种字符串方法或者函数可以帮我鉴定一下一个字符串是否是另一个大字符串的一部分?

#!/usr/bin/env python
def contain(str1,str2):
if str1 in str2:
print "str1 contains str2"
else:
print "str1 not contains str2"
if __name__=="__main__":
str1=raw_input("please type the string1:")
str2=raw_input("please type the string2:")
contain(str1,str2)

6-2 字符串标识符.修改例 6-1 的 idcheck.py 脚本,使之可以检测长度为一的标识符,并且可以识别 Python 关键字,对后一个要求,你可以使用 keyword 模块(特别是 keyword.kelist)来帮你.

#!/usr/bin/env python
import string
import keyword
alphas=string.letters+'_'
nums=string.digits
keywords=keyword.kwlist
print 'Welcome to the Identifier Checker v1.0'
print 'Testees must be at least 2 chars long.'
myInput=raw_input('Identifier to test?')
if myInput in keywords:
print "%s can not be the keyword"%myInput
elif len(myInput)>1:
if myInput in keywords:
print "chars can not contain the keywords"
if myInput[0] not in alphas:
print '''invalid:first symbol must be alphabetic'''
else:
for otherChar in myInput[1:]:
if otherChar not in alphas +nums:
print '''invalid:remaining symbols must be alphanumeric'''
break
else:
print "okay as an identifier"
else:
print "The word is illegal identifier for python"
6-3  排序 

(a) 输入一串数字,从大到小排列之.

#!/usr/bin/env python
#-*-coding:utf-8-*-
string=raw_input("please enter the string:")
print sorted(string)

(b) 跟 a 一样,不过要用字典序从大到小排列之.

#!/usr/bin/env python
string =raw_input("please enter the number:")
list =string.split()
print list
print sorted(list)

  

Python核心编程2第六章课后练习的更多相关文章

  1. 《Python核心编程》 第六章 序列 - 课后习题

    课后习题 6–1.字符串.string 模块中是否有一种字符串方法或者函数可以帮我鉴定一下一个字符串是否是另一个大字符串的一部分? 答:成员关系操作符(in.not in) import string ...

  2. Python核心编程2第四章课后练习

    4-1 Python 对象.与所有 Python 对象有关的三个属性是什么?请简单的描述一下.      身份:对象的唯一标识      类型 :对象的类型决定了该对象可以保存什么类型的值       ...

  3. python核心编程2 第六章 练习

    6-2. 字符串标识符.修改例 6-1 的 idcheck.py 脚本,使之可以检测长度为一的标识符,并且可以识别 Python 关键字,对后一个要求,你可以使用 keyword 模块(特别是 key ...

  4. Python核心编程2第五章课后练习

    5-1 整型,讲讲python普通整型与长整型区别 python整形一共有三种:布尔型,长整型和标准整型.普通整型与长整型的区别在于标准整形的取值范围是-2^31到2^31-1,长整型所能表达的数值与 ...

  5. Python核心编程2第三章课后练习

    1. 标识符.为什么Python 中不需要变量名和变量类型声明? Python中的变量不需要声明,变量的赋值操作既是变量声明和定义的过程.每个变量在内存中创建,都包括变量的标识,名称和数据这些信息.每 ...

  6. 【windows核心编程】 第六章 线程基础

    Windows核心编程 第六章 线程基础 欢迎转载 转载请注明出处:http://www.cnblogs.com/cuish/p/3145214.html 1. 线程的组成 ①    一个是线程的内核 ...

  7. python核心编程2 第十一章 练习

    11-2 函数.结合你对练习5-2的解,以便你创建一个带一对相同数字并同时返回它们之和以及产物的结合函数. multiply = lambda x, y: x * y if __name__ == ' ...

  8. 《Python核心编程》 第五章 数字 - 课后习题

    课后习题  5-1 整形. 讲讲 Python 普通整型和长整型的区别. 答:普通整型是绝大多数现代系统都能识别的. Python的长整型类型能表达的数值仅仅与你机器支持的(虚拟)内存大小有关. 5- ...

  9. 《Python核心编程》 第四章 Python对象- 课后习题

    练习 4-1. Python对象.与所有Python对象有关的三个属性是什么?请简单的描述一下. 答:身份.类型和值: 身份:每一个对象都有一个唯一的身份标识自己,可以用id()得到.  类型:对象的 ...

随机推荐

  1. java 哈希码

    加入新的元素到数组中 /** * Append the given object to the given array, returning a new array * consisting of t ...

  2. centos7 mysql 5.6.30 默认配置文件

      默认配置 vim /etc/my.cnf.rpmsave mysql  Ver 14.14 Distrib 5.6.30, for linux-glibc2.5 (x86_64) using  E ...

  3. JAVA学习.java.sql.date 与java.util.date以及gettime()方法的分析

    java.sql.Date 是针对SQL语句使用的,它只包含日期而没有时间部分. java.util.Date 就是在除了SQL语句的情况下面使用. 它都有getTime方法返回毫秒数,返回的是自19 ...

  4. DataGridView 列大写、列只能输入数字 分类: DataGridView 2014-12-07 08:40 332人阅读 评论(0) 收藏

    列大写: 说明:调用EditingControlShowing事件 private void dgvGoods_EditingControlShowing(object sender, DataGri ...

  5. 设置UWP程序自启动(Automate launching Windows 10 UWP apps)

    在开发UWP程序的过程中,有时候需要设置程序的自启.本人实现的步骤如下: 1.在VS中激活Protocol (Package.appxmanifest --> Declarations --&g ...

  6. VS2010+Opencv-2.4.9的配置攻略

    1.下载软件 vs2010入门书籍,免积分下载   http://download.csdn.net/detail/u014112584/7325617 opencv2.4.0版本号和一些样例,免积分 ...

  7. mysql 参数:[read_buffer_size] [sort_buffer_size] [read_rnd_buffer_size] [tmp_table_size]---图解

    http://imysql.cn/2008_09_27_deep_into_mysql_sort_buffer http://my.oschina.net/realfighter/blog/36442 ...

  8. etrace跟踪Nginx代码+ FASTCGI

    http://blog.csdn.net/jianqiangchen/article/details/29175285 http://blog.csdn.net/jianqiangchen/artic ...

  9. Apple-Watch开发2 APPIcon设置

    网址:http://makeappicon.com/ 直接放置到相应的文件即可 (these are all in pixels) 48 x 48 55 x 55 58 x 58 80 x 80 87 ...

  10. 根据引用jar包路径查找原JAR包

    网站:http://www.findjar.com/. 就是这个网站,经常在网上看到一些好的源码,什么都说了,就是没有说明需要引入那个包,这个包需要从什么地方下载,有些时候在网上搜索还不一定搜索得到, ...