数字及字符串

python中的数据类型#

标准类型:数字 字符串 列表 元祖 字典
其他类型:类型type null 文件 集合 函数/方法 类

数字:
特性:
1.只能存放一个值
2.一经定义,不可更改
3.直接访问

分类:整形,长整型,布尔,浮点,复数

python3中长整型统一为整形


布尔:
True 和False

浮点数float:
1.2e-5=0.000012
1.23*109=1.23e9

数字的内建函数:
int:当传入参数为小数,直接去掉小数部分 int(3.5) 3
round:四舍五入取小数 round(3.5) 4
math.floor:;类似int 取离愿小数最近但小于最近但大于原小数的数math.ceil(3.1) 4
long:长整形工厂函数
float:浮点型工厂函数
complex:复数工厂函数
bool:布尔型工厂函数 传入参数为非空 非零 非none 均返回True
abs:取绝对值 abs(-1) 1
coerce:接受两个参数,把数据转成相同类型,返回一个元祖 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**3
hex(hexadecimal):十进制转十六进制 hex(10)='0xa'
oct(octonary):十进制转八进制  oct(10)='012'
ord:根据ascii将字符转成十进制 ord('a')=97
chr:根据ascii将十进制转成字符 chr('97')=a


字符串
定义:它是一个有序的字符的集合,用于存储表示基本的文本信息 ''或""或""" """ 均为字符串
特性:
1.只能存放一个值
2.不可变
3.按照从左到右的顺序定义字符集合,下标从0开始顺序访问 有序
4.字符串的单引号和双引号都无法取消特殊字符的含义,如果想让引号内所有字符均取消特殊含义需要在前面加r,如name=r'l	hf'
5.unicode字符串与r连用必须在r前面如:name=ur'l	hf'

重点
移除空白: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]])  ->int
str.index(sub[,start[,end]]) ->int
str.isalnum() -> bool 至少一个字符,且都是字母或数字返回True
str.isalpha() ->bool 至少一个字符,且都是字母返回True
str.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, 表示保留
, False不保留
str.replace(old, new[, count])  替换count个old为新的new,count默认为全部
str.swapcase()转换字符串中的每一个字母的大小写



# print(1.3e-3)
# print(1.3e3)

#二进制 10/2
print(bin(10))
#八进制 10/8  12
print(oct(10))
#十六进制 0-9 a b c d e f
print(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  4


x='*******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 3
print(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 4

a='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