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. Unity通过NTP获取网络时间

    最初通过qq时间服务器获得时间,经常出现有网络也获取失败的情况. 后面寻找解决办法,查找资料终于发现通过ntp时间服务器获取网络时间的方法.   首先游戏开始获得初始化网络时间,通常只获取一次,其他时 ...

  2. 高性能MySQL之基础架构

    一.背景 为什么我们需要先学习MYSQL的基础架构先呢? 原因很简单,当我们需要了解一件事物的时候,我们只有站在宏观的层面,才能层层剥丝抽茧的去理解问题.举个例子,我们要看一个框架的源码,一开始就想进 ...

  3. Gin + Vue全栈开发实战(二)

    尝试地写了第一篇自己学习Go Web框架的感受和入门的文章,发现反响还不错,大家也提出了很多的问题来一起交流.近期也渐渐地出现了很多有关go语言开发的相关文章,包括有在蚂蚁金服的大牛的分享,我也一直有 ...

  4. DNS解析综合学习案例

    DNS解析综合学习案例 #图右侧为做题前环境配置 #命令为红色 #命令加载内容为绿色 #vi编辑内容为蓝色 1.用户需把/dev/myvg/mylv逻辑卷以支持磁盘配额的方式挂载到网页目录下 [roo ...

  5. Java网络编程与NIO详解8:浅析mmap和Direct Buffer

    微信公众号[黄小斜]作者是蚂蚁金服 JAVA 工程师,目前在蚂蚁财富负责后端开发工作,专注于 JAVA 后端技术栈,同时也懂点投资理财,坚持学习和写作,用大厂程序员的视角解读技术与互联网,我的世界里不 ...

  6. C# - 协变、逆变 看完这篇就懂了

    1. 基本概念 官方:协变和逆变都是术语,前者指能够使用比原始指定的派生类型的派生程度更大(更具体的)的类型,后者指能够使用比原始指定的派生类型的派生程度更小(不太具体的)的类型.[MSDN] 公式: ...

  7. unity_小功能实现(敌人巡逻功能)

    利用NavMeshAgent控制敌人巡逻,即敌人在一组位置间循环巡逻. 首先我们要知道NavMeshAgent中有两个方法:1.锁定当前巡逻的某一目标位置,即navMeshAgent.destinat ...

  8. 使用css实现水平垂直居中

    1.通过absolute和margin实现(适用于弹窗,具体位置随浏览器屏幕大小变化改变)这种方式需要居中元素的父级必须采用绝对定位或相对定位,被居中元素的尺寸需要固定. <div class= ...

  9. 红黑树以及与AVL树的区别

    http://blog.csdn.net/zwan0518/article/details/12219055 http://blog.csdn.net/v_july_v/article/details ...

  10. leetcode8 String to Integer

    题目描述 Implement atoi which converts a string to an integer. The function first discards as many white ...