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. JavaWeb——使用会话维持状态3

    这次的例子是使用会话给上一个例子添加登陆功能 1.页面逻辑 首先是登陆页面,这里需要输入账号和密码,输入正确后将进入商品列表页面,输入错误将会提示账号或者密码错误 其次是商品列表和购物车页面,添加了注 ...

  2. Spring入门(七):Spring Profile使用讲解

    1. 使用场景 在日常的开发工作中,我们经常需要将程序部署到不同的环境,比如Dev开发环境,QA测试环境,Prod生产环境,这些环境下的一些配置肯定是不一样的,比如数据库配置,Redis配置,Rabb ...

  3. hadoop2.7之作业提交详解(上)

    根据wordcount进行分析: import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; impo ...

  4. 【KakaJSON手册】06_Model转JSON

    前面的文章介绍了如何利用KakaJSON进行JSON转Model,从这篇文章开始介绍如何将Model转成JSON 生成JSON和JSONString struct Car: Convertible { ...

  5. 前端利器躬行记(2)——Babel

    Babel是一个JavaScript编译器,不仅能将当前运行环境不支持的JavaScript语法(例如ES6.ES7等)编译成向下兼容的可用语法(例如ES3或ES5),这其中会涉及新语法的转换和缺失特 ...

  6. Flutter学习笔记(24)--SingleChildScrollView滚动组件

    如需转载,请注明出处:Flutter学习笔记(23)--多 在我们实际的项目开发中,经常会遇到页面UI内容过多,导致手机一屏展示不完的情况出现,以Android为例,在Android中遇到这类情况的做 ...

  7. springboot报 org.thymeleaf.exceptions.TemplateInputException: Error resolving template "succeed";

    --------------------- 本文转自 林晓风 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/Lin_xiaofeng/article/details/ ...

  8. Liunx学习总结(四)--文件的权限管理

    文件和目录的权限 每个文件都有其所有者(u:user).所属组(g:group)和其他人(o:other)对它的操作权限,a:all则同时代表这3者.权限包括读(r:read).写(w:write). ...

  9. 使用WPF为Powershell程序制作GUI界面

    1. 使用Xaml创建应用界面 打开visual studio,创建一个新的项目,在已安装模板中选择Visual C# →Wpf应用. 完成创建后,我们得到如下图所示的应用界面. wpf界面是基于xa ...

  10. 【社群话题分享】当 Bug 引发事故时,是否应该追究责任呢?

    每周三下午的话题活动是又拍云技术社群的优良传统- 大家一起来看看这周都聊了些啥吧- 看看之前大家还聊了些什么,点击下方你感兴趣的话题~ [社群话题分享]是世界上最_语言? [社群话题分享]IDE 技术 ...