1.1.1python基本数据类型之数字和字符串

点击跳转Python学习笔记总目录

一,数字和字符串类型

1.bin()函数将十进制转换成而进制

byte = bin(123)
print(byte)
#运行结果
0b1111011

2.oct()函数将十进制转换成八进制

oct_s = oct(123)
print(oct_s)
#运行结果
0o173

3.hex()函数将十进制转换成十六进制,十六进制表示:0-9 a b c d e f

hex_s = hex(123)
print(hex_s)
#运行结果
0x7b

4.数字类型的特性:

只能存放一个值
一经定义,不可更改
直接访问
分类:int,float,complex

5.字符串类型

引号包含的都是字符串类型
  单双引号没有区别

    s1='hello world'  
	s="hello world"
	s2="""hello world"""  
	s3='''hello world''' 

6.字符串的常用操作

strip() #移除空白,也可以去除其他的字符
s = '   123   '
new_s = s.strip()
print(new_s)
#运行结果
'123'
split() #分割,默认以空格分割。也可以以其他的字符分割
s = '1,2,3,4,5'
new_s = s.split(',')  #以逗号分割字符串
print(new_s)
#运行结果
['1', '2', '3', '4', '5']
len() #长度,返回字符串的长度
s = '1213'
lenth = len(s)
print(lenth)
# 运行结果
4
[::]字符串切片操作[起始:终点:步长]
s = '12345'
new = s[0:3]  #切片操作,顾头不顾尾
print(new)
#运行结果
'123'
s = '123456789'
new = s[0:8:2]  #位置0-7,步长为2
print(new)
#运行结果
'1357'

capitalize() #首字母大写

s = 'python'
new_s = s.capitalize()
print(new_s)
#运行结果
'Python'
center() #居中显示例如:x=‘hello’ print(x.center(30,’#’))
s = 'python'
print(s.center(30,'-'))  # 宽度30,python居中,空白位'-'填充,'-'可空可任意设置
#运行结果
------------python------------
count() #计数,顾头不顾尾,统计某个字符的个数,空格也算一个字符
s = '12311'
num = s.count('1')  #统计字符串s中有几个1
print(num)
#运行结果
3
endswith() #以什么结尾
s = '12311'
end = s.endswith('1')  #判断s是否以1结尾
print(end)
#运行结果
true
satrtswith() #以什么开头
s = '12311'
start = s.startswith('1')
print(start)
#运行结果
true
find() #查找字符的索引位置,如果是负数,代表查找失败
s = '12311'
ret = s.find('3')  #查找s中3所在的索引
print(ret)
#运行结果
2
index() #功能同find,查索引—区别(find找不到返回-1,index找不到报错)
s = '12311'
ret = s.find('3')
print(ret)
#运行结果
2

format()字符串格式化
msg = 'name:{},age:{},sex:{}'
s = msg.format('haiyan', 18, '女')
print(s)
#运行结果
'name:haiyan,age:18,sex:女'

msg = 'name:{0},age:{1},sex:{0}'
s1 = msg.format('aaaaaa', 'bbbbbb')
print(s1)
#运行结果
'name:aaaaaa,age:bbbbbb,sex:aaaaaa'

msg = 'name:{x},age:{y},sex:{z}'
s2 = msg.format(x='haiyan', y='18', z='女')
print(s2)
#运行结果
'name:haiyan,age:18,sex:女'
字符串常用格式化方法%s,%d
s = '我叫%s,今年%s岁了,我爱%s' %('毛毛腿','18','睡觉')
print(s)
# 运行结果
'我叫毛毛腿,今年18岁了,我爱睡觉'
isdigit()判断是否是数字
s = '123'
ret = s.isdigit()
print(ret)
#运行结果
true
islower()判断是否是全部小写
s = 'python'
ret = s.lower()
print(ret)
#运行结果
true
isupper()判断是否是全部大写
s = 'PYTHON'
ret = s.isupper()
print(ret)
#运行结果
true
lower()全部转换为小写
s = 'Python'
ret = s.lower()
print(ret)
#运行结果
'python'
upper()全部转换为大写
s = 'Python'
ret = s.upper()
print(ret)
#运行结果
'PYTHON'
isspace()判断是否是全都是空格
s = '    '
ret = s.isspace()
print(ret)
#运行结果
true
istitle()判断是否是标题(首字母大写)
s = 'Python'
ret = s.istitle()
print(ret)
#运行结果
True
swapcase()大小写字母翻转
s = 'Python'
ret = s.swapcase()
print(ret)
#运行结果
'pYTHON'
join()插入
s = 'Python'
ret = s.join('123456')
print(ret)
#运行结果
1Python2Python3Python4Python5Python6
repalce()替换
s = 'Python'
ret = s.replace('P','IP')
print(ret)
#运行结果
'IPython'
ljust()左对齐
s = 'Python'
ret = s.ljust(10,'-')
print(ret)
#运行结果
Python----
rjust() 右对齐
s = 'Python'
ret = s.rjust(10,'-')
print(ret)
#运行结果
----Python

猜你喜欢

转载自blog.csdn.net/weixin_39726347/article/details/86532713