博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
数字及字符串
阅读量:4451 次
发布时间:2019-06-07

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

python中的数据类型#标准类型:数字 字符串 列表 元祖 字典其他类型:类型type null 文件 集合 函数/方法 类数字:特性:1.只能存放一个值2.一经定义,不可更改3.直接访问分类:整形,长整型,布尔,浮点,复数python3中长整型统一为整形布尔:True 和False浮点数float:1.2e-5=0.0000121.23*109=1.23e9数字的内建函数:int:当传入参数为小数,直接去掉小数部分 int(3.5) 3round:四舍五入取小数 round(3.5) 4math.floor:;类似int 取离愿小数最近但小于最近但大于原小数的数math.ceil(3.1) 4long:长整形工厂函数float:浮点型工厂函数complex:复数工厂函数bool:布尔型工厂函数 传入参数为非空 非零 非none 均返回Trueabs:取绝对值 abs(-1) 1coerce:接受两个参数,把数据转成相同类型,返回一个元祖 coerce(-1,3.2)(-1.0,3.2)divmod:返回元祖,包含商和余数 divmod(93,10) 得(9,3)pow:取平方 pow(2,3,3)相当于2**3%3得2 pow(2,3)等同于2**3hex(hexadecimal):十进制转十六进制 hex(10)='0xa'oct(octonary):十进制转八进制  oct(10)='012'ord:根据ascii将字符转成十进制 ord('a')=97chr:根据ascii将十进制转成字符 chr('97')=a字符串定义:它是一个有序的字符的集合,用于存储表示基本的文本信息 ''或""或""" """ 均为字符串特性:1.只能存放一个值2.不可变3.按照从左到右的顺序定义字符集合,下标从0开始顺序访问 有序4.字符串的单引号和双引号都无法取消特殊字符的含义,如果想让引号内所有字符均取消特殊含义需要在前面加r,如name=r'l\thf'5.unicode字符串与r连用必须在r前面如:name=ur'l\thf'重点移除空白:strip()分割:split()长度:len()索引:index()切片:string[1:2]字符串操作str.capitalize()  首字母变大写str.casefold()  casefold函数可识别更多的对象将其输出为小写,而lower函数只能完成ASCII码中A-Z之间的大写到小写的转换str.center(width[,fillchar]) 字符居中 空格补全str.count(sub[,start[,end]]) -> int 一个范围内统计某str出现的次数str.startswith/endswith() 开始结束str.find(sub[,start[,end]])  ->intstr.index(sub[,start[,end]]) ->intstr.isalnum() -> bool 至少一个字符,且都是字母或数字返回Truestr.isalpha() ->bool 至少一个字符,且都是字母返回Truestr.isdecimal() -> bool 字符串是否只包含十进制字符str.isdight()  -> bool  字符串是否全是数字str.isidentifier() ->bool 是否为Python中的标识符str.islower()/isupper() -> 是否为大小写str.isnumeric() ->bool  是否只由数字组成str.space() -> 是否为空格str.istitle() -> 是否为标题(首字母大写)str.join(iterable) 字符串加入可迭代对象str.ljust/rjust(width[,fillchar]) 与center类似 分别向左向右str.split(sep=None, maxsplit=-1)分割字符串,指定sep为分隔符,maxsplit为最大分隔符。0表示不分割,1表示分割成2段str.splitlines([keepends]): keepends为True, 表示保留\n, False不保留str.replace(old, new[, count])  替换count个old为新的new,count默认为全部str.swapcase()转换字符串中的每一个字母的大小写# print(1.3e-3)# print(1.3e3)#二进制 10/2print(bin(10))#八进制 10/8  12print(oct(10))#十六进制 0-9 a b c d e fprint(hex(10))# name=input("username")# print(type(name))# name=name.strip()# #去掉左右2边的字符#x='********egon**********'# x=x.strip('*')# print(x)#x='hello'# print(x.capitalize())# print(x.upper())# print(x.center('30','#')) #居中显示# print(x.count('l',0,3))print(x.startswith())print(x.endswith())msg='name:{},age:{},sex:{}'msg1='name:{0},age:{1},sex:{0}'msg2='name:{x},age:{y},sex:{z}'print(msg.format('egon',18,'mail'))print(msg1.format('aaaaa','bbbb'))print(msg2.format(x='egon',y=18,z='male'))print(x.find('e') )x='hello world'print(x[0])print(x[4])print(x[5])print(x[100])#超出范围print(x[-1])print(x[-3])print(x[1:3])print(x[1:5:2])#步长x='hello'print(x.index('o'))print(x[4])print(x[x.index('o')])x='123'print(x.isdigit())age=input('age:')if age.isdigit():    new_age=int(age)    print(new_age,type(new_age))msg5='hello alex'print(msg5.replace('x','X'))print(msg5.replace('l','n'))print(msg5.replace('l','A',2))x='root:x:0::0:/root:/bin/bash'print(x.split(':'))x='hello'print(x.upper())x='HELLO'print(x.isupper())print(x.lower())print(x.islower())x=''print(x.isspace()) #全是空格# print(x.islower())# print(x.isspace())x='abc'print(x.ljust(10,'x')) #左对齐print(x.rjust(10,'x'))#右对齐# print(x.ljust)# print(x.replace())# print(x.split())msg='Hello'print(msg.title())print(msg.istitle())x='aB'print(x.swapcase())需要掌握msg='hello'移除空白msg.strip()分割msg.split()长度len(msg)索引msg[0:5:2]"""msg='   hello  '#移除空白print(msg.strip())msg1='root:x:0::0:/root:/bin/bash'#分割print(msg1.split(':'))#长度print(len(msg1))#索引print(msg1[3])#切片print(msg[0:5:2]) #0  2  4x='*******egon********'print(x.strip('*'))y='hello'#首字母大写print(y.capitalize())#所有字母大写print(y.upper())#居中显示print(y.center(20,'$'))#统计某个字符个数print(y.count('l'))print(y.count('l',0,4)) #0 1 2 3print(y.startswith('h'))print(y.endswith('e'))#字符串格式化msg1='name:{},age:{},gender:{}'print(msg1.format('egon',18,'male'))msg2='name:{0},age:{1},gender:{0}'print(msg2.format('morgana',25))msg3='name:{x},age:{y},gender:{z}'print(msg3.format(y=18,x='egon',z='male'))#字符串切片z='hello world'print(z[0])#print(z[100])#报错print(z[1:5:2]) #0 1 2 3 4a='hello'print(a.index('o'))print(a[4])print(a[a.index('o')])# age=input('input your age')# if age.isdigit():#     new_age=int(age)#     print(new_age,type(new_age))msg4='hello alex'print(msg4.replace('x','X'))print(msg4.replace('l','A',2))b='hello'print(b.isupper())print(b.islower())print(b.isspace())print(b.istitle())print(b.swapcase())print(b.title())#左对齐print(b.ljust(10,'*'))#有对齐print(b.rjust(10,'*'))c='Ab'#反转print(c.swapcase())

  

转载于:https://www.cnblogs.com/morgana/p/6959540.html

你可能感兴趣的文章
fis webpack 原理对比
查看>>
22 广播的发送
查看>>
Linux 创建用户 限制SFTP用户只能访问某个目录
查看>>
正则表达式的学习笔记
查看>>
android图片特效处理之图片叠加
查看>>
结束贪心hdu 2491 Priest John's Busiest Day
查看>>
RHEL7中防火墙firewalld基础使用配置
查看>>
编程漫谈(八):此刻的幸福
查看>>
Python实现Json结构对比的小工具兼谈编程求解问题
查看>>
Java入门之:基本数据类型
查看>>
导航属性
查看>>
指针函数与函数指针
查看>>
Git工作流总结
查看>>
什么时候修改class
查看>>
冒泡与捕获的理解
查看>>
Jira客户端
查看>>
BZOJ1192: [HNOI2006]鬼谷子的钱袋
查看>>
shell之变量字符串的操作
查看>>
centos的网络配置
查看>>
clone git 项目到 非空目录
查看>>