今日内容:

1、数字的基本操作

2、字符串的操作及常用方法

3、列表的操作及常用方法

重点:

1、字符串的操作及常用方法

  (1)常用操作

"""
字符串的操作:
"""
s = "This is a test,and include letter、numbers 1,2,3 and up-letter ABcDe aBCdE"
# 1、根据索引取值 ---> s[]
letter = s[5]
print(letter) # i # 2、对字符串进行切片 --> s[start_index:end_index:step]
sp = s[::]
print(sp) # This is a test,and include letter、numbers 1,2,3 and up-letter ABcDe aBCdE
sp1 = s[:5:]
print(sp1) # This
sp2 = s[10::]
print(sp2) # test,and include letter、numbers 1,2,3 and up-letter ABcDe aBCdE
sp3 = s[5:10:]
print(sp3) # is a
sp4 = s[5:15:2]
print(sp4) # i et
spre = s[-1:-10:-1]
print(spre) # EdCBa eDc # 3、字符串的遍历
for i in s:
print(i,end=" ") # T h i s i s a t e s t , a n d i n c l u d e l e t t e r 、 n u m b e r s 1 , 2 , 3 a n d u p - l e t t e r A B c D e a B C d E
print() # 4、字符串的长度 ---> len()
s_lenth = len(s)
print(s_lenth) # # 5、成员运算 ---> in \ not in
print("h" in s) # True # # 6、字符串的删除 ---> 直接清空字符串所在的空间,删除的是数据本身,不是绑定关系
# s1 = "haha 这是一个要被删除的字符串"
# del s1
# print(s1) # NameError: name 's1' is not defined

  (2)字符串的常用方法

"""
字符串的重要方法
"""
s_zh = "***This is a test,and include letter、numbers 1,2,3 and up-letter ABcDe aBCdE,并且包括了中文---**" # 1、 去除空白及指定字符 ---> strip() \ lstrip() \ rstrip()
s2 = s_zh.strip()
print(s2) # ***This is a test,and include letter、numbers 1,2,3 and up-letter ABcDe aBCdE,并且包括了中文---**
print(id(s2),id(s_zh)) # 2407212780880 2407212574624
print(s_zh.strip("*")) # This is a test,and include letter、numbers 1,2,3 and up-letter ABcDe aBCdE,并且包括了中文---
print(s_zh.lstrip("*")) # This is a test,and include letter、numbers 1,2,3 and up-letter ABcDe aBCdE,并且包括了中文---**
print(s_zh.rstrip("*")) # ***This is a test,and include letter、numbers 1,2,3 and up-letter ABcDe aBCdE,并且包括了中文--- # 2、 将字符串切分成列表 ---> split() \ rsplit("str","count"):对字符串进行切分,变为列表,默认以空白进行切分依据!
print(s_zh.split()) # ['***This', 'is', 'a', 'test,and', 'include', 'letter、numbers', '1,2,3', 'and', 'up-letter', 'ABcDe', 'aBCdE,并且包括了中文---**']
print(s_zh.rsplit()) # ['***This', 'is', 'a', 'test,and', 'include', 'letter、numbers', '1,2,3', 'and', 'up-letter', 'ABcDe', 'aBCdE,并且包括了中文---**']
print(s_zh.split(" ")) # ['***This', 'is', 'a', 'test,and', 'include', 'letter、numbers', '1,2,3', 'and', 'up-letter', 'ABcDe', 'aBCdE,并且包括了中文---**']
print(s_zh.split("a",1)) # ['***This is ', ' test,and include letter、numbers 1,2,3 and up-letter ABcDe aBCdE,并且包括了中文---**']
print(s_zh.rsplit("a",1)) # ['***This is a test,and include letter、numbers 1,2,3 and up-letter ABcDe ', 'BCdE,并且包括了中文---**'] # 3、将特定字符串替换成另一种字符串 ---> replace("old","new",count)
print(s_zh.replace(" ","%")) # ***This%is%a%test,and%include%letter、numbers%1,2,3%and%up-letter%ABcDe%aBCdE,并且包括了中文---**
print(s_zh.replace(" ","%",2)) # ***This%is%a test,and include letter、numbers 1,2,3 and up-letter ABcDe aBCdE,并且包括了中文---** # 4、将列表中每个值添加进字符串中 ---> "str".join(intrable) :以 . 为分界将 interable 中每个元素组合成字符串
ls = ["我","需要","将","这个","列表","添加","进","字符串中"]
print("".join(ls)) # 我需要将这个列表添加进字符串中
print(".".join(ls)) # 我.需要.将.这个.列表.添加.进.字符串中 # 5、统计字符串中特定字符出现的次数 ---> count(str, start_index=None, end_index=None)
print(s_zh.count("i")) # # 6、根据字符取索引 ---> index(str[, start[, end]])
print(s_zh.index("i")) # 5 --> 从左到右第一个 # 7、 以特定字符开头及结尾 ---> startwith(str[, start[, end]]) / endwith(str[, start[, end]])
print(s_zh.startswith("*")) # True
print(s_zh.endswith("-")) # False # 8、判断字符串内部是否是自然数、纯字母、字符串或字母、汉字 ---> isdigit() / isalpha() / isalnum()
# isdigit --> 判断是否为纯数字(自然数,不包括罗马数字、汉字等)
# isalpha --> 判断是否为纯字母或汉字
# isalnum --> 判断是否为字母或汉字或数字(所有类型的数字)
# isnumeric --> 判断是否为数字(所有类型的数字) print("Ⅳ".isnumeric()) # True
print("Ⅳ".isdigit()) # False
print("Ⅳ".isalnum()) # True
print("a1".isalpha()) # False # 9、字符串的大写小写 --> upper \ lower \ captialize \ title \
s_zh1 = "this is a test,and include letter、numbers 1,2,3 and up-letter abcde abcde"
print(s_zh1.upper()) # ***THIS IS A TEST,AND INCLUDE LETTER、NUMBERS 1,2,3 AND UP-LETTER ABCDE ABCDE,并且包括了中文---**
print(s_zh1.lower()) # ***this is a test,and include letter、numbers 1,2,3 and up-letter abcde abcde,并且包括了中文---**
print(s_zh1.capitalize()) # This is a test,and include letter、numbers 1,2,3 and up-letter abcde abcde
print(s_zh1.title()) # This Is A Test,And Include Letter、Numbers 1,2,3 And Up-Letter Abcde Abcde

2、列表操作及其常用方法

  (1)常用操作

"""
基本操作
"""
ls = [1,2,3,"a",'b','c',"赵",'钱'] # 1、取值
print(ls[2]) # # 2、切片
print(ls[::-1]) # ['钱', '赵', 'c', 'b', 'a', 3, 2, 1] # 3、拼接
print(ls + ["",2,3]) # [1, 2, 3, 'a', 'b', 'c', '赵', '钱', '1', 2, 3] # 4、长度
print(len(ls)) # # 5、成员运算 in \ not in
# 6、遍历:使用for循环,循环遍历列表

  (2)列表的常用方法

"""
常用方法
"""
# 1、增
ls.append("append")
print(ls) # [1, 2, 3, 'a', 'b', 'c', '赵', '钱', 'append'] ls.insert(3,"insert")
print(ls) # [1, 2, 3, 'insert', 'a', 'b', 'c', '赵', '钱', 'append'] ls.extend([100,200,300])
print(ls) # [1, 2, 3, 'insert', 'a', 'b', 'c', '赵', '钱', 'append', 100, 200, 300] # 2、删
ls.remove("append")
print(ls) # [1, 2, 3, 'insert', 'a', 'b', 'c', '赵', '钱', 100, 200, 300] ls.pop(3) # --> 可以删除指定index的值,默认删除最后一个
print(ls) # [1, 2, 3, 'a', 'b', 'c', '赵', '钱', 100, 200, 300] # ls.clear() # 将列表中值进行删除 # 3、改
ls[1] = 22
print(ls) # [1, 22, 3, 'a', 'b', 'c', '赵', '钱', 100, 200, 300] # 4、查
print(ls.index("赵")) #
print(ls.count("a")) # 1 --> 查询指定字符串出现次数 # 5、其它方法
ls.reverse()
print(ls) # [300, 200, 100, '钱', '赵', 'c', 'b', 'a', 3, 22, 1] ls1 = ['a','d','c','f','e','g']
ls1.sort(reverse=True)
print(ls1) # ['g', 'f', 'e', 'd', 'c', 'a'] --> 只能对同种类型的数据进行排序 # ls.copy() --> 浅拷贝,只是复制列表容器本身及内部的内存地址,不会复制列表中元素本身

其它内容:

1、数字的操作

数字类型直接的相互转化
a = 10
b = 3.74
c = True
print(int(a), int(b), int(c)) # 10 3 1
print(float(a), float(b), float(c)) # 10.0 3.74 1.0
print(bool(a), bool(b), bool(c)) # True True True
 

      

  

day05 数据类型的方法详解的更多相关文章

  1. Python数据类型及其方法详解

    Python数据类型及其方法详解 我们在学习编程语言的时候,都会遇到数据类型,这种看着很基础也不显眼的东西,却是很重要,本文介绍了python的数据类型,并就每种数据类型的方法作出了详细的描述,可供知 ...

  2. python的dict()字典数据类型的方法详解以及案例使用

    一.之前的回顾 # int  数字 # str 字符串 # list 列表 # tuple 元组 # dict 字典 字典中最重要的方法 keys() values() items() get upd ...

  3. python的list()列表数据类型的方法详解

    一.列表 列表的特征是中括号括起来的,逗号分隔每个元素,列表中的元素可以是数字或者字符串.列表.布尔值......等等所有类型都能放到列表里面,列表里面可以嵌套列表,可以无限嵌套 字符串的特征是双引号 ...

  4. C++调用JAVA方法详解

    C++调用JAVA方法详解          博客分类: 本文主要参考http://tech.ccidnet.com/art/1081/20050413/237901_1.html 上的文章. C++ ...

  5. Clone使用方法详解【转载】

    博客引用地址:Clone使用方法详解 Clone使用方法详解   java“指针”       Java语言的一个优点就是取消了指针的概念,但也导致了许多程序员在编程中常常忽略了对象与引用的区别,本文 ...

  6. integer与int区别以及integer.values()方法详解

    声明:本文为博主转载文章,原文地址见文末. 知识点1:integer和int的区别 /* * int是java提供的8种原始数据类型之一.Java为每个原始类型提供了封装类,Integer是java为 ...

  7. $.ajax()方法详解 jquery

    $.ajax()方法详解   jquery中的ajax方法参数总是记不住,这里记录一下. 1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type: 要求为Str ...

  8. jQuery中 $.ajax()方法详解

    $.ajax()方法详解 jquery中的ajax方法参数总是记不住,这里记录一下. 1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type: 要求为Strin ...

  9. [五]基础数据类型之Short详解

      Short 基本数据类型short  的包装类 Short 类型的对象包含一个 short 类型的字段      原文地址:[五]基础数据类型之Short详解   属性简介   值为  215-1 ...

随机推荐

  1. Python--day10(函数(使用、分类、返回值))

    1.  函数 1.  函数: 完成特定功能的代码块,作为一个整体,对其进行特定的命名,该名字就代表这函数 现实中:很多问题要通过一些工具进行处理 => 可以将工具提前生产出来并命名 =>通 ...

  2. MyEclipse不自动编译问题

    没图,别找了... 我在MyEclipse上从SVN中导项目,导下的项目跑不起来,发现tomcat的classes中是空文件夹. 以下是在网上找的其他方法: 1.确保:Project->buil ...

  3. JS 设计模式五 -- 命令模式

    概念 命令模式中的命令(command) 指的是 一个执行某些待定事情的指令. 用一种松耦合的方式来设计程序,使得请求发送者和请求接收者能够消除彼此之间的耦合关系. 例子 假设html结构如下: &l ...

  4. CentOS系统版本的查看方法

    CentOS系统版本的查看方法 查看操作系统版本 1 [root@aliyun ~]# lsb_release -a LSB Version: :core-4.1-amd64:core-4.1-noa ...

  5. Django(六)Session、CSRF、中间件

    大纲 二.session 1.session与cookie对比 2.session基本原理及流程 3.session服务器操作(获取值.设置值.清空值) 4.session通用配置(在配置文件中) 5 ...

  6. bugku crypto 告诉你一个秘密(ISCCCTF)

    emmmm....有点坑 题目: 636A56355279427363446C4A49454A7154534230526D6843 56445A31614342354E326C4B4946467A57 ...

  7. django rest framework serializers

    django rest framework serializers序列化   serializers是将复杂的数据结构变成json或者xml这个格式的 serializers有以下几个作用:- 将qu ...

  8. ubuntu14.04按照mysql5.7

    1.安装mysql5.5 https://www.cnblogs.com/zhuyp1015/p/3561470.html https://www.cnblogs.com/ruofengzhishan ...

  9. Linux 系统从入门到精通的学习大纲;

    以前没有接触过Linux,生产环境需要,有时候遇到问题,百度一下,问题解决了,在遇到问题,在百度,有时候问题是如何解决的,为什么会解决有点丈二的和尚摸不着头脑, 为此,想用一段时间,系统的学习下Lin ...

  10. Linux系统 Cetos 7 中重置root密码

    几个月前在自己电脑上面安装了一个Linux 的虚拟机环境,当时是为了测试某一个小功能,用完就扔那里了,长时间没有使用,发现Root密码忘记了,登陆不了,怎么办呢?(ps:如果实际情况中忘记密码的这个服 ...