Strings可以想象成一个有序列的数组

#Indexing

planet = 'Pluto'

planet[0]

'P'

#Slicing

planet[-3:]

'uto'

#How long

len(planet)

String methods

#字母大写

claim = 'Pluto is a planet'

claim.upper()

#all lowercase

claim.lower()

#search for the first index of substring

claim.index('plan')

Going between strings and lists : .splits and .join()

words = claim.split()

datestr='1956-01-31'

year,month,day = datestr.split('-')

'/'.join ([month,day,year])

'/'.join([word.upper() for word in words])

如过数值是non-string object,则可以先使用str 转换数值

str(position)

format占位符

This is getting hard to read and annoying to type. str.format() to the rescue

"{}. you'll always be the{}th planet to me".format{planet,position}

检测元组中是否有数值:

seq = ['one', 'two', 'three']

if 'one' in seq:
  print True

Dictionraries

numbers={'one':1, 'two':2, 'three': 3}

numbers['one']

1

numbers['eleven']=11

numbers

练习一:

A researcher has gathered thousands of news articles. But she wants to focus her attention on articles including a specific word. Complete the function below to help her filter her list of articles.

Your function should meet the following criteria

- Do not include documents where the keyword string shows up only as a part of a larger word. For example, if she were looking for the keyword “closed”, you would not include the string “enclosed.”
- She does not want you to distinguish upper case from lower case letters. So the phrase “Closed the case.” would be included when the keyword is “closed”
- Do not let periods or commas affect what is matched. “It is closed.” would be included when the keyword is “closed”. But you can assume there are no other types of punctuation.

Answer:

Solution:

def word_search(doc_list, keyword):
  indices = []
  for i,doc in enumerate(doc_list):
    temp=doc.split()
    word = [item.strip('.,').lower() for item in temp]
    if keyword.lower() in word:
    indices.append(i)
  return indices

Python学习之String的更多相关文章

  1. [python学习笔记] String格式化

    格式化 S % (args...) 方式 特点 str里的占位符同java里的占位符. 优势 这种方式可以限定格式化的时候接受的数据类型. 常见占位符 %d 接收数字,格式化为 十进制 %x 接收数字 ...

  2. Python 学习小结

    python 学习小结 python 简明教程 1.python 文件 #!/etc/bin/python #coding=utf-8 2.main()函数 if __name__ == '__mai ...

  3. Python 学习文章收藏

    作者 标题 rollenholt Python修饰器的函数式编程 - Rollen Holt - 博客园 rollenholt python操作gmail - Rollen Holt - 博客园 ro ...

  4. Python学习记录day6

    title: Python学习记录day6 tags: python author: Chinge Yang date: 2016-12-03 --- Python学习记录day6 @(学习)[pyt ...

  5. Python学习记录day5

    title: Python学习记录day5 tags: python author: Chinge Yang date: 2016-11-26 --- 1.多层装饰器 多层装饰器的原理是,装饰器装饰函 ...

  6. python学习笔记系列----(八)python常用的标准库

    终于学到了python手册的最后一部分:常用标准库.这部分内容主要就是介绍了一些基础的常用的基础库,可以大概了解下,在以后真正使用的时候也能想起来再拿出来用. 8.1 操作系统接口模块:OS OS模块 ...

  7. Python学习路程day16

    Python之路,Day14 - It's time for Django 本节内容 Django流程介绍 Django url Django view Django models Django te ...

  8. 记录Python学习中的几个小问题

    记录Python学习中的几个小问题,和C#\JAVA的习惯都不太一样. 1.Django模板中比较两个值是否相等 错误的做法 <option value="{{group.id}}&q ...

  9. 180分钟的python学习之旅

    最近在很多地方都可以看到Python的身影,尤其在人工智能等科学领域,其丰富的科学计算等方面类库无比强大.很多身边的哥们也提到Python非常的简洁方便,比如用Django搭建一个见得网站只需要半天时 ...

随机推荐

  1. ThreadPoolExecutor线程池的一个面试题

    问题:现有一个线程池,参数corePoolSize = 5,maximumPoolSize = 10,BlockingQueue阻塞队列长度为5,此时有4个任务同时进来,问:线程池会创建几条线程? 如 ...

  2. mybatis学习笔记(一)

    mybatis学习笔记 mybatis简介 Mybatis 开源免费框架.原名叫iBatis,2010在googlecode,2013年迁移到 github 作用: 数据访问层框架,底层对JDBC进行 ...

  3. Mysql 分页order by一个相同字段,发现顺序错乱

    两次分页查询,其中跳过了2个id   select * from jdp_tb_trade  where jdp_modified>='2017-04-24 20:22:01' and jdp_ ...

  4. 【0802 | Day 7】Python进阶(一)

    目 录  数字类型的内置方法 一.整型内置方法(int) 二.浮点型内置方法(float) 字符串类型内置方法 一.字符串类型内置方法(str) 二.常用操作和内置方法 优先掌握: 1.索引取值 2. ...

  5. 读取某个目录下的所有图片并显示到pictureBox

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  6. .Net MVC 框架基础知识

    一.什么是MVC? MVC 是一种使用 MVC(Model View Controller 模型-视图-控制器)设计创建 Web 应用程序的模式. MVC全名是Model View Controlle ...

  7. 在linux中部署项目并创建shell脚本

    1.首先要在idea中父工程maven包下执行clean生成的target包 2.执行package打包,打包时候讲test勾去掉 3.将target包中生成的jar包cp出来 此处注意打包时必须要保 ...

  8. jquery实现表格导入到Excel(加图片)

      话不多说直接上代码 第一步:导入jquery的插件https://github.com/rainabba/jquery-table2excel HTML部分: 第二步:添加一个按钮 <but ...

  9. 开发APP必须知道的API集合,来源http://www.cnblogs.com/wikiki/p/7232388.html

    笔记 OneNote - OneNote支持获取,复制,创建,更新,导入与导出笔记,支持为笔记添加多媒体内容,管理权限等.提供SDK和Demo. 为知笔记 - 为知笔记Windows客户端开放了大量的 ...

  10. #第 12 篇:解锁博客侧栏,GoGoGo!

    作者:HelloGitHub-追梦人物 文中涉及的示例代码,已同步更新到 HelloGitHub-Team 仓库 我们的博客侧边栏有四项内容:最新文章.归档.分类和标签云.这些内容相对比较固定和独立, ...