一Python的数据类型可以分为可变与不可变两种:

可变类型:值改变,但是id不变,证明就是在改变原值,就是可变类型

如list   dict 列表和字典都是可变类型

不可变类型:值改变,id也跟着改变了,证明就是不可变类型,入

Int  float  str都是不可变类型

一,字符串类型

1、用途: 性别\爱好等描述性质的状态
 2、定义方式
# s1="hello" # s1=str('hello')
#str 可以将任意其他类型都转成str类型
# res=str({'a':1}) #res="{'a':1}"
# 3、常用操作+内置的方法
#优先掌握的操作:(*****)
#1、按索引取值(正向取+反向取) :只能取

取值正向取标号是从0开始递增,反向取标号是从-1开始递减

s1="hello world"
# print(s1[0])
# print(s1[-1])
# print(s1[-3])
# s1[0]='H'
# print(s1)
#2、切片(顾头不顾尾,步长):从大字符串中切出一个子字符串
# s1="hello world"
# res=s1[1:5]
# print(s1)
# print(res)
# print(s1[0:7:1]) #0 1 2 3 4 5 6
# print(s1[0:7:2]) #0 2 4 6
# 取值之后用加号运算符也可以实现切片的功能
# print(s1[-1]+s1[-2]+s1[-3]+s1[-4]+s1[-5])
# print(s1[-1::-1]) # -1 -2
# print(s1[::-1]) # -1 -2

#3、长度len
# s1="hello world"
# print(len(s1)) # 字符的个数

#4、成员运算in和not in:判断一个子字符串是否存在于一个大字符串中
# msg='my name is alex,alex is dsb'
# print('alex' in msg)
# print('egon' not in msg)

#5、移除空白strip: 移除字符串左右两边的字符空格
# name=input('username>>>: ').strip() #name='egon '
# name=name.strip()
# if name == 'egon':
#     print('认证成功')

# msg='    he    llo     
'
# res=msg.strip(' ')
# print(msg)
# print(res)

# msg='******hello*************'
# res=msg.strip('*')
# print(res)

# msg='***&^#***hello***=-/?**'
# print(msg.strip('*&^$/-?#='))

#6、切分split: 把一個有規律的字符串按照某個字符進行切分,切成列表
# info='root:x:0:0::/root:/bin/bash'
# res=info.split(':',maxsplit=-1)
# print(res)
# cmd='get|a.txt|3333'
# res=cmd.split('|')
# print(res)

# info=''
# userinfo=['root', 'x', '0', '0', '', '/root', '/bin/bash']
# for item in userinfo:
#     item+=':'
#     info+=item
# info=info.strip(':')
# print(info,type(info))

# userinfo=['root', 'x', '0', '0', '', '/root', '/bin/bash']
# res=':'.join(userinfo)
# print(res,type(res))
#7、循环
# msg='hello'
# for item in msg:
#     print(item)

# 需要掌握的操作(****)
#1、strip,lstrip,rstrip
# print('****egon****'.strip('*'))
# print('****egon****'.lstrip('*'))
# print('****egon****'.rstrip('*'))

#2、lower,upper
# x='ABBBBddd1231'
# print(x.lower())
# print('ABBBBddd2123'.upper())

#3、startswith,endswith
# print('alex is sb'.startswith('alex'))
# print('alex is sb'.startswith('al'))
# print('alex is sb'.endswith('sb'))

#4、format的三种玩法  这个参数要一一对应,中间用逗号隔开
# msg='my name is %s my age is %s' %('egon',18)
# print(msg)

# msg='my name is {name} my age is {age}'.format(age=18,name='egon')
# print(msg)

# 了解
# msg='my name is {} my age is {}'.format(18,'egon')
# print(msg)
# msg='my name is {0} my age is {0}{1}{1}'.format(18,'egon')
# print(msg)

# x1='egon'
# x2=('egon111')
# print(x1,x2,type(x1),type(x2))

#5、split,rsplit
# print('a:b:c:d:e'.split(':',maxsplit=1))
# print('a:b:c:d:e'.rsplit(':',maxsplit=1))

#6、join

#7、replace
# msg='alex is alex hahahah alex'
# res=msg.replace('alex','SB',1)
# print(msg)
# print(res)

#8、isdigit
# print('1010101'.isdigit())
# age=input('>>>: ')
# if age.isdigit():
#     age=int(age)
#     if age > 10:
#         print('too Big')
#     elif age < 10:
#         print('too small')
#     else:
#         print('you got it')
# else:
#     print('必須輸入數字')

# 其他操作(了解即可)
#1、find,rfind,index,rindex,count
# print("abcdefg".find('de',0,3))
# print("abcdefg".index('de'))
# print("abcdefg".index('de',0,3))

# print('alex is alex'.find('alex'))
# print('alex is alex'.rfind('alex'))

# print('alex is alex'.count('alex'))

#2、center,ljust,rjust,zfill
# print('================%s===============' %('egon'))
# print('egon'.center(50,'*'))
# print('egon'.ljust(50,'*'))
# print('egon'.rjust(50,'*'))
# print('egon'.zfill(50))

#3、expandtabs
# print('abc\tdef'.expandtabs(8))

#4、captalize,swapcase,title
# print('i am egon'.capitalize())
# print('aAbB'.swapcase())
# print('i am egon'.title())

#5、is数字系列
num1=b'4' #bytes
num2=u'4' #unicode,python3中无需加u就是unicode
num3='壹' #中文数字
num4='Ⅳ' #罗马数字

# ''.isdigit() # bytes,unicode
# print(num1.isdigit())
# print(num2.isdigit())
# print(num3.isdigit())
# print(num4.isdigit())

# ''.isdecimal():unicode
# print(num2.isdecimal())
# print(num3.isdecimal())
# print(num4.isdecimal())
# ''.isnumeric():unicode,羅馬,中文
# print(num2.isnumeric())
# print(num3.isnumeric())
# print(num4.isnumeric())

#6、is其他
# name='egon123'
# print(name.isalnum()) #字符串由字母或数字组成
# name='egon'
# print(name.isalpha()) #字符串只由字母组成
# print(name.islower())
# print(name.isupper())
# print(name.isspace())
# print(name.istitle()

Python记录2:数据类型的更多相关文章

  1. (八)python的简单数据类型和变量

    什么是数据类型? 程序的本质就是驱使计算机去处理各种状态的变化,这些状态分为很多种. 例如英雄联盟游戏,一个人物角色有名字,钱,等级,装备等特性,大家第一时间会想到这么表示 名字:德玛西亚------ ...

  2. 第三篇:python基础之数据类型与变量

    阅读目录 一.变量 二.数据类型 2.1 什么是数据类型及数据类型分类 2.2 标准数据类型: 2.2.1 数字 2.2.1.1 整型: 2.2.1.2 长整型long: 2.2.1.3 布尔bool ...

  3. python中基本数据类型以及运算符

    python中基本数据类型以及运算符的知识 一.与用户的交互以及python2与python的区别 1.1什么是与用户交互 用户交互就是人往计算机中input(输入数据),计算机print(输出结果) ...

  4. 第二篇.1、python基础之数据类型与变量

    一.变量 1 什么是变量之声明变量 #变量名=变量值 age=18 gender1='male' gender2='female' 2 为什么要有变量 变量作用:“变”=>变化,“量”=> ...

  5. Python 入门之数据类型之间的相互转换 以及 在编程中会遇到的数据类型的坑

    Python 入门之数据类型之间的相互转换 以及 在编程中会遇到的数据类型的坑 1.数据类型总结: 可变,不可变,有序,无序 (1)可变的数据类型:list dict set (2)不可变的数据类型: ...

  6. python 基础之数据类型

    一.python中的数据类型之列表 1.列表 列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储.修改等操作 二.列表常用操作 >切片>追加>插入>修改& ...

  7. Python学习 之 数据类型(邹琪鲜 milo)

    1.Python中的数据类型:数字.字符串.列表.元组.字典 2.数字类型包括整型.长整型.浮点型.复数型 type(number):获取number的数据类型 整型(int):范围:-2,147,4 ...

  8. Python基础之数据类型

    Python基础之数据类型 变量赋值 Python中的变量不需要声明,变量的赋值操作既是变量声明和定义的过程. 每个变量在内存中创建,都包括变量的标识,名称和数据这些信息. 每个变量在使用前都必须赋值 ...

  9. Python学习之数据类型

    整数 Python可以处理任意大小的整数,在程序中的表示方法和数学上的写法一模一样,例如:1,100,-8080,0,等等. 用十六进制表示整数比较方便,十六进制用0x前缀和0-9,a-f表示,例如: ...

  10. python的组合数据类型及其内置方法说明

    python中,数据结构是通过某种方式(例如对元素进行编号),组织在一起数据结构的集合. python常用的组合数据类型有:序列类型,集合类型和映射类型 在序列类型中,又可以分为列表和元组,字符串也属 ...

随机推荐

  1. Mac终端的Cocoapods的安装及使用

    阅读目录 第一步,首先要检查Mac是否安装了rvm.打开终端,输入指令 rvm -v 第二步,用rvm安装ruby环境 第三步,检查更新RubyGems(Ruby1.9.1 以后的版本自带RubyGe ...

  2. airflow docker

    https://github.com/puckel/docker-airflow 镜像介绍:https://hub.docker.com/r/puckel/docker-airflow/ docker ...

  3. LeetCode 1012 Complement of Base 10 Integer 解题报告

    题目要求 Every non-negative integer N has a binary representation.  For example, 5 can be represented as ...

  4. 新建虚拟机_WIN8 64位系统_启动报错Directory "EZBOOT" not found

    准备工作:下载win8 64 镜像文件 1.虚拟机安装win8 64位操作系统,新建虚拟机步骤同XP系统 2.BIOS设置CD/ROM启动,但启动报错,如下,由于镜像文件超过4G,无法从虚拟机安装,需 ...

  5. 洛谷P3242 接水果 [HNOI2015] 整体二分

    正解:整体二分+树状数组 解题报告: 传送门! 题目还是大概解释下?虽然其实是看得懂的来着,,, 大概就是说给一棵树.给定一些询问,每个询问都是说在两个点之间的路径上的子路径的第k大是什么 然后看到这 ...

  6. 重读《深入理解Java虚拟机》六、Java泛型 VS C#泛型 (伪泛型 VS 真泛型)

    一.泛型的本质 泛型是参数化类型的应用,操作的数据类型不限定于特定类型,可以根据实际需要设置不同的数据类型,以实现代码复用. 二.Java泛型 Java 泛型是Java1.5新增的特性,JVM并不支持 ...

  7. oracle中is和as的区别

    在存储过程(PROCEDURE)和函数(FUNCTION)中没有区别:在视图(VIEW)中只能用AS不能用IS:在游标(CURSOR)中只能用IS不能用AS.

  8. [MySQL优化2]不用SELECT * FROM table;

    假设有一张employees表,它有8列:员工人数,姓氏,名字,分机,电子邮件,办公室代码,报告,职位等.如果要仅查看员工的名字,姓氏和职位,请使用以下查询:SELECT lastname, firs ...

  9. win10安装pycharm及汉化包

    PyCharm 是一款功能强大的 Python 编辑器,带有一整套可以帮助用户在使用Python语言开发时提高其效率的工具,那么如何安装pycharm呢?都是英文看不懂有没有汉化版呢?跟ytkah一起 ...

  10. GENIL_BOL_BROWSER, GENIL_MODEL_BROWSER,BSP_WD_CMPWB 使用方法

    一:GENIL_BOL_BROWSER 使用方法 1: 进入x3c系统.输入T-CODE  GENIL_BOL_BROWSER 2: 输入一个component set  名称 3: 选择一个对象,双 ...