博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
第8课 对象的方法
阅读量:4568 次
发布时间:2019-06-08

本文共 3023 字,大约阅读时间需要 10 分钟。

一、对象的方法

1、python中一切类型的数据都是对象。

  1)对象:包含属性和方法(行为)。举例:人的身高、体重是属性;吃饭、睡觉是行为。

2、count:计算字符串中包含多少个指定的子字符串

>>> a = '123 123 456 789'>>> a.count('123')2

3、startswith():检查字符串是否以指定的子字符串开头

tel = input('请输入需要查询的手机号码:')if len(tel) == 11:    if tel.isdigit():        if tel.startswith('187') or tel.startswith('136'):            print('中国移动!')        elif tel.startswith('131') or tel.startswith('132'):            print('中国联通!')        elif tel.startswith('181') or tel.startswith('189'):            print('中国电信!')        else:            print('无该号段!')    else:        print('含有非法字符!')else:    print('手机号码长度有误!')

 4、endswith():检查字符串是否以指定的子字符串结尾。

 5、find():返回指定的子字符串在字符串中出现的位置(返回下标)。

  1)注意:如果查找的元素有多个,只返回第一个元素的下标

>>> a = '1213 456 789 xyz'>>> a.find('x')13
>>> a = '1213 456 789 xyz'>>> a.find('xy')   # 只返回x的下标13

   2)可以指定开始查找的位置

>>> a = '123 678 9 x,y,z'>>> a.find('y', 2) # 2为指定查找的开始位置,但是'y'在字符串中的位置是不变的,所以不管开始位置从哪里开始(只要不是从z开始,因为这样找不到),返回结果都一样。12

  6、isalpha():检查字符串中是否都是字母。

>>> b = 'yyzz, 123, 6789'>>> b.isalpha()False
>>> b = 'zyzzbc'>>> b.isalpha()True

 7、isdigit():检查字符串中是否都是数字。

>>> a = '1234567890xyz'>>> a.isdigit()False>>> a = '1236789'>>> a.isdigit()True

8、str.join():sequence类型的元素字符串和并到一个字符串,string作为分割符。

>>> str1 = ['my', 'name', 'is', 'tom']>>> '_'.join(str1)'my_name_is_tom'

9、split():将字符串分割为几个子字符串,参数为分隔符。返回结果存放在一个list对象里面。

>>> str1 = 'abc.def.xyz.ly'>>> str1.split('.')['abc', 'def', 'xyz', 'ly']

10、lower():将字符串里面的所有大写字母转化为小写。

>>> str1 = 'Hello Python'>>> str1.lower()'hello python'

11、upper():将字符串里面的所有小写字母转为大写。

>>> str1 = 'hello world'>>> str1.upper()'HELLO WORLD'

12、replace():替换字符串里面的子字符串---全部替换。str1.replace('原字符串', '替换后的字符串')

>>> str1 = 'aaabbbcccffffyyyyzzzzwwww'>>> str1.replace('a', 'l')'lllbbbcccffffyyyyzzzzwwww'

13、strip():将字符串前置空格和后置空格删除,但不能去掉中间空格。

>>> str1 = '   my name is tom      '>>> str1.strip()'my name is tom'

  1)lstrip():将字符串左边空格删除

>>> str1 = '  hello world   '>>> str1.lstrip()'hello world   '

  2)rstrip():将字符串右边的空格删除

>>> str1 = '    hello python    '>>> str1.rstrip()'    hello python'

 

二、列表

1、append():给列表增加一个元素,从尾部增加。

>>> list1 = ['a', 'b', 'tom']>>> list1.append('python')>>> list1['a', 'b', 'tom', 'python']

2、insert():给列表指定位置插入一个值。格式:list1.insert(位置,  要插入的值)

>>> list1 = ['hello world', 'hello python']>>> list1.insert(1, 'hello java')>>> list1['hello world', 'hello java', 'hello python']

3、del:删除列表的元素。格式:del 列表名[元素下标]

>>> alist = [1, 2, 3, 4]>>> del alist[3]>>> alist[1, 2, 3]

4、pop():在删除元素的同时,会得到元素的值。格式:列表.pop(元素下标)

>>> list1 = [1,2,6,7,8,9]>>> list1.pop(1)2

5、remove():删除列表的元素,直接通过值删除,不是通过下标。格式:列表.remove(元素值)。这种方法最慢

>>> list1 = ['x','y','z',1,2,3]>>> list1.remove('x')>>> list1['y', 'z', 1, 2, 3]

6、全部删除。clear()和指向空列表。区别如下:

  1)alist.clear():删除所有元素。

  2)alist = [] :将alist指向一个空列表

>>> alist = [1,2,3,4,5,6,7,8,9,0]>>> alist.clear()>>> alist[]
>>> alist = [1,2,3,4]>>> alist = []>>> alist[]

7、reverse():将列表的元素倒序排列

>>> alist = [1,2,3,4,5,6,7,8,9,0]>>> alist.reverse()>>> alist[0, 9, 8, 7, 6, 5, 4, 3, 2, 1]

 

三、学会查看文档

1、学好英文

2、使用chrome浏览器

 

转载于:https://www.cnblogs.com/nick1998/p/10016167.html

你可能感兴趣的文章
无尽的控件-GridView复合表头
查看>>
Luogu4726 【模板】多项式指数函数(NTT+多项式求逆)
查看>>
e3mall商城的归纳总结2之认识dubbo、zookeeper
查看>>
纯js实现图片上传
查看>>
嵌入式SQL
查看>>
HDOJ(HDU) 2133 What day is it(认识下Java的Calendar类---日期类)
查看>>
甲级1002 A+B for Polynomials (25)
查看>>
centos部署flask
查看>>
hdu 4507 吉哥系列故事——恨7不成妻
查看>>
C与C++ 无参函数的区别
查看>>
WPF DesiredSize & RenderSize
查看>>
快速开发第一个SpringBoot应用
查看>>
表中有A B C三列,用SQL语句实现:当A列大于B列时选择A列否则选择B列
查看>>
HTML video标签 兼容总结
查看>>
锡瓦塔内霍 墨西哥 / 巴克斯顿 /
查看>>
css+html应用实例1:滑动门技术的简单实现
查看>>
C++智能指针 auto_ptr
查看>>
Direct3D 索引缓存
查看>>
Eclipse开发环境的配置
查看>>
Java集合框架的学习
查看>>