1、rstrip()删除末尾指定字符串

例如:A = '1,2,3,4,5,'

   B = A.rstrip(',')

   B = '1,2,3,4,5'

2、isdigit()方法

Python isdigit() 方法检测字符串是否只由数字组成,返回 True 或者False。

例如:str = '123456'

   str.isdigit()为True

3、enumerate() 函数

enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。

例如:seq = ['one', 'two', 'three']

   for i, element in enumerate(seq):

     print i, element

输出:0 one

   1 two

   2 three

4、字符串 startswith、find、join

例如:name = 'Swaroop'

   if name.startswith('Swa'):  #startswith方法用于查找字符串是否以给定的字符串内容开头

    print('the string start with "Swa"')

   if 'a' in name:

    print('it contains the string "a"')

   if name.find('war') !=-1:  #find方法用于定位字符串中给定字符串的位置,如果找不到对应的字符串,会返回-1

    print('it contains the string "war"')

   delimiter = '_*_'

   mylist = ['Brazil','Russia','India','China']

   print(delimiter.join(mylist))  #字符串delimiter将会作为每个项目之间的分隔,并以此返回一串更大的字符串

输出:the string start with "Swa"

   it contains the string "a"

   it contains the string "war"

   Brazil_*_Russia_*_India_*_China

5、函数传递元组,返回多个变量

例如:def run():

    return ('hello','world')

   first, second = run()

   print(first)

   print(second)

输出:hello

   world

6、在函数中接收元组与字典

分别使用 * 或 ** 作为元组或字典的前缀,来使它们作为一个参数为函数所接收

例如:def powersum(power, *args):

    total = 0

    for i in args:total += pow(i, power)

    return total

   print(powersum(2, 3, 4))

   print(powersum(2, 10))

输出:25

   100

python:零散记录的更多相关文章

  1. python 零散记录(七)(下) 新式类 旧式类 多继承 mro 类属性 对象属性

    python新式类 旧式类: python2.2之前的类称为旧式类,之后的为新式类.在各自版本中默认声明的类就是各自的新式类或旧式类,但在2.2中声明新式类要手动标明: 这是旧式类为了声明为新式类的方 ...

  2. python 零散记录(七)(上) 面向对象 类 类的私有化

    python面向对象的三大特性: 多态,封装,继承 多态: 在不知道对象到底是什么类型.又想对其做一些操作时,就会用到多态 如 'abc'.count('a') #对字符串使用count函数返回a的数 ...

  3. python 零散记录(五) import的几种方式 序列解包 条件和循环 强调getattr内建函数

    用import关键字导入模块的几种方式: #python是自解释的,不必多说,代码本身就是人可读的 import xxx from xxx import xxx from xxx import xx1 ...

  4. python 零散记录(二) 序列的相关操作 相加 相乘 改变 复制

    序列相加: [1,2] + [3,4] == [1,2,3,4] #字符串也是序列的一种 'hello' + 'world' == 'hello world' #但是序列相加只限于相同类型的序列间相加 ...

  5. python 零散记录(一) input与raw_input 数学相关函数 转换字符串的方法

    input()与raw_input(): 两者都是接受命令行输入,但区别在于,raw_input()接受原始数据(raw data). #使用input()来提示输入名字 input("en ...

  6. python 零散记录(六) callable 函数参数 作用域 递归

    callable()函数: 检查对象是否可调用,所谓可调用是指那些具有doc string的东西是可以调用的. 函数的参数变化,可变与不可变对象: 首先,数字 字符串 元组是不可变的,只能替换. 对以 ...

  7. python 零散记录(四) 强调字典中的键值唯一性 字典的一些常用方法

    dict中键只有在值和类型完全相同的时候才视为一个键: mydict = {1:1,':1} #此时mydict[1] 与 mydict['1']是两个不同的键值 dict的一些常用方法: clear ...

  8. python 零散记录(三) 格式化字符串 字符串相关方法

    使用 % 符号格式化字符串: """常用转换说明符:""" #%s: 按照str()方式转换 #%r: 按照repr()方式转换 #%d: ...

  9. Python学习记录day6

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

  10. Python学习记录day5

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

随机推荐

  1. 前端代码规范(转载 http://codeguide.bootcss.com/)

    http://codeguide.bootcss.com/ HTML 语法 HTML5 doctype 语言属性(Language attribute) 字符编码 IE 兼容模式 引入 CSS 和 J ...

  2. linux的目录结构详细介绍

    linux的目录结构详细介绍 1. /目录(根目录) 2./ect/目录 特定主机系统范围内的配置文件. 3./usr/目录 默认软件都会存于该目录下.用于存储只读用户数据的第二层次:包含绝大多数的用 ...

  3. 用eclipse-inst-win64.exe安装eclipse出现Java for Windows Missing 的原因

    Java for Windows Missing 因为jdk的版本没有对,我这里是64位的机器上安了32位的jdk,所以一直报这个. 必须换上相对应版本的jdk,提示页面有链接,直接点击就可以下载. ...

  4. C# 对象复制

    /// <summary> /// 把DataTable对象转成List<T>对象 /// </summary> /// <typeparam name=&q ...

  5. spring mvc 通过拦截器记录请求数据和响应数据

    spring mvc 能过拦截器记录请求数据记录有很多种方式,主要有以下三种: 1:过滤器 2:HandlerInterceptor拦截器 3:Aspect接口控制器 但是就我个人所知要记录返回的数据 ...

  6. AJPFX关于对集合中的元素删除操作和注意点

    import java.util.ArrayList;import java.util.Iterator;import java.util.List; public class ForeTest2 { ...

  7. 牛人cad二次开发网站(.net)

    http://through-the-interface.typepad.com/through_the_interface/autocad_net/ http://through-the-inter ...

  8. python的与或非运算

    真的很重要,栽了个跟头!!!(虽然以前好像知道...) print(True or False and False) print((True or False) and False) # True # ...

  9. poj3262 Protecting the Flowers

    思路: 简单贪心,每次选择性价比最高的. 实现: #include <iostream> #include <cstdio> #include <algorithm> ...

  10. springdata-jpa 八种查询方法

    使用:maven+Spring+jpa+Junit4 查询方式:SQL,JPQL查询,Specification多条件复杂查询 返回类型:list<POJO>,list<Stinrg ...