博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
数据类型内置方法之数据类型与字符串类型
阅读量:6799 次
发布时间:2019-06-26

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

数据类型内置方法

数字类型内置方法

整型

  • 1.用途:年龄、身份证号码、手机号。。。
  • 2.定义方式:
age1 = 18age2 = int(18)print(age1)print(id(age1))print(type(age1))print(type(age2))
181723559936
  • 3.常用操作和内置方法:+ - * / // ** % ;无内置方法
  • 4.存一个值or多个值:一个值
  • 5.有序or无序(有索引为有序,无索引为无序):根本没有有序或无序的说法
  • 6.可变or不可变:不可变
#不可变类型:值变则ID变age = 18print(id(age))age += 1print(id(age))
17235599361723559968
#可变类型:值变ID则不变list1 = ['name','height','weight']print(list1)print(id(list1))list1[1] = 185print(list1)print(id(list1))
['name', 'height', 'weight']1701728445128['name', 185, 'weight']1701728445128

浮点型

  • 1.用途:身高、体重、薪水、存款
  • 2.定义方式:
salary1 = 1.5salary2 = float(1.5)print((salary1))print(id(salary1))print(type(salary1))print(type(salary2))
1.51701727543560
  • 3.常用操作和内置方法:+ - * / ** // % :无内置方法
  • 4.存一个值or多个值: 一个值
  • 5.有序or无序:根本没有有序无序一说
  • 6.可变or不可变:不可变

字符串类型内置方法

  • 1.用途:名字、单个爱好
  • 2.定义方式:
name1 = 'william'name2 = "william"name3 = '''william1william2'''name4 = """william3william4"""print(type(name1))print(type(name4))
  • 3.常用操作和内置方法:
    1.优先掌握
    *1.按索引取值
my_info_list = ['name','height', 'weight','hobby',['reading','running']]my_info = "name height weight hobby ['reading','running']"print(my_info_list[2])print(my_info[2])print(my_info_list[4])print(my_info[4])print(my_info[5])
weightm['reading', 'running'] h

***2.切片(顾头不顾尾),步长的正负控制行进的方向,左行进还是右行进。

my_info = "name height weight hobby ['reading','running']"print(my_info[:])print(my_info[0:6])print(my_info[0:8:2])print(my_info[-1:-4:-1])print(my_info[-4:-1])print(my_info[-4:])
name height weight hobby ['reading','running']name hnm e]'gng'ng']

***3.长度len

my_info = "name height weight hobby ['reading','running']"print(len(my_info))
46

***4.成员运算 in   not in

my_info = "name height weight hobby ['reading','running']"print('height' in my_info)print('hobbies'in my_info)
TrueFalse

***5.移除空白strip,默认为空格

test = '  a abc   a'print(test.strip())print(test.strip('a'))
a abc   a  a abc

***6.切分split

my_info = "name height weight hobby ['reading','running']"print(my_info.split())print(my_info.split(' ',2))print(my_info.split('i'))
['name', 'height', 'weight', 'hobby', "['reading','running']"]['name', 'height', "weight hobby ['reading','running']"]['name he', 'ght we', "ght hobby ['read", "ng','runn", "ng']"]

***7.循环

my_info = "name height weight hobby ['reading','running']"for i in my_info :    print(i,end=' ')
n a m e   h e i g h t   w e i g h t   h o b b y   [ ' r e a d i n g ' , ' r u n n i n g ' ]

2.需要掌握

*1.lstrip & rstrip

test = '   a  abc  a'print(test.lstrip())print(test.lstrip('a'))print(test.rstrip('a'))
a  abc  a   a  abc  a   a  abc

***2.lower & upper

my_info = "Name Height Weight hobby ['reading','running']"print(my_info.lower())print(my_info.upper())
name height weight hobby ['reading','running']NAME HEIGHT WEIGHT HOBBY ['READING','RUNNING']

***3.startswith & endswith

my_info = "name height weight hobby ['reading','running']"print(my_info.startswith('name'))print(my_info.startswith('he'))print(my_info.endswith('running'))
TrueFalseFalse

***4.rsplit

my_info = "name height weight hobby ['reading','running']"print(my_info.rsplit(' '))print(my_info.rsplit(' ',2))
['name', 'height', 'weight', 'hobby', "['reading','running']"]['name height weight', 'hobby', "['reading','running']"]

***5.join

my_info = "name height weight hobby ['reading','running']"my_info_split = my_info.split()print(my_info)print(my_info_split)print(''.join(my_info_split))print('.'.join(my_info_split))
name height weight hobby ['reading','running']['name', 'height', 'weight', 'hobby', "['reading','running']"]nameheightweighthobby['reading','running']name.height.weight.hobby.['reading','running']

***6.replace

my_info = "name height weight hobby ['reading','running']"print(my_info.replace('name','william').replace('weight','150'))
william height 150 hobby ['reading','running']

***7.isdigit

print('10dsf'.isdigit())print('10'.isdigit())
FalseTrue

**3.了解

# find \ rfind \ index \ rindex \ counthis_info = 'Name 1 WHEIGHT reading running reading 1 height'print(his_info.find('1'))  print(his_info.find('18'))  #记住不是False,而是-1print(his_info.rfind('1'))print(his_info.rfind('z'))  #记住不是False,而是-1
5-139-1
his_info = 'Name 1 WHEIGHT reading running reading 1 height'print(his_info.index('WHEIGHT'))#print(his_info.index('18'))  #会报错
7
his_info = 'Name 1 WHEIGHT reading running reading 1 height'print(his_info.count('reading'))
2
# center \ ljust \ rjust \ zfillprint('SS'.center(20,'*'))print('SS'.center(20,'+'))print('SS'.ljust(20,'*'))print('SS'.rjust(20,'+'))print('SS'.zfill(20))
*********SS*********+++++++++SS+++++++++SS******************++++++++++++++++++SS000000000000000000SS
# expandtabsprint('\tTom'.expandtabs(4))
Tom
#capitalize \ swapcase \ titleprint('tom and jerry'.capitalize())print('Tom and Jerry'.swapcase())print('tom and jerry'.title())
Tom and jerrytOM AND jERRYTom And Jerry
# is系列print('tom'.isdigit())print('tom'.isalpha())print('10'.isalpha())
FalseTrueFalse
  • 4.存一个值or多个值:一个值
  • 5.有序or无序:有序
  • 6.可变or不可变:不可变
name = 'William'print(name)print(id(name))name += '2'print(name)print(id(name))
William1701731277152William21701731197360

转载于:https://www.cnblogs.com/WilliamKong94/p/10829347.html

你可能感兴趣的文章
JDBC的基本用法
查看>>
Android开发之TextView排版问题
查看>>
9.0 alpha 版安装出现 could not execute command lessc 的问题
查看>>
SIP入门(二):建立SIPserver
查看>>
html里的table如何在表格内部保留表格横线的同时去掉表格里的竖线
查看>>
老板必备:核心员工跳槽时,必聊的8个话题(转)
查看>>
TNS-00512: Address already in use-TNS-12542: TNS:address already in use
查看>>
什么是快速排序(转)
查看>>
会议论文重新投稿算不算侵权?这肯定是所多人都遇到过的问题。
查看>>
js判断checkbox状态,处理表单提交事件
查看>>
工程师,请优化你的代码
查看>>
BZOJ3495 : PA2010 Riddle
查看>>
探访莱布尼茨:与大师穿越时空的碰撞
查看>>
Hibernate SQL优化技巧dynamic-insert="true" dynamic-update="true"
查看>>
如何削减高速语言?
查看>>
山寨游戏的未来Apple App Store
查看>>
JSON.parse()和JSON.stringify()
查看>>
python出现UnicodeEncodeError有可能产生的另一个原因
查看>>
域名绑定,解析总结
查看>>
JAVA 网络长短连接
查看>>