数据类型及内置方法

数据类型及内置方法

一、数字类型

1.int整型      (不可变类型,一个值)

print(int(3.1))
# res=int('1111111')
# print(res,type(res))

整型可接收纯数字组成的字符串、浮点型或者是阿拉伯数字

在python中十进制转换为二进制   bin(x)

                                转换为八进制   oct(x)

转换为16进制 hex(x)

2.float浮点型      (不可变类型,一个值)

同int   

# res=float('111111.1')
# print(res,type(res))
二、str 字符串类型      (有序、不可变类型)

类型转换:任意类型数据都可以转换成字符串类型

需精通的常用操作+内置方法:

  1. 按索引取值(正向取+反向取):只能取出,不能输入

    # res=float('111111.1')
    # print(res,type(res))

    2.切片(顾头不顾尾,步长)

msg='hello world'
print(msg[0:5:2]) 
# 0表示起始字符位置,5表示终止字符位置,2表示间隔长度
print(msg[0:5])#没有步长就按顺序输出
print(msg[::-1])#步长为负数从后往前,反转字符串的方法
print(msg[0:])
print(msg[-1:-6:-1])
#若想从后往前取,必须起止位置与步长一致
print(msg[:])
hlo
hello
dlrow olleh
hello world
dlrow
hello world

3.长度  len

msg='bkwrflag'
n=len(msg)
print(n)
#len表示字符串的长度

4.成员运算  in 和  not in

in:判断一个字符串是否在一个大字符串中

msg='hello world'
print('he'in msg)
print('wx'not in msg)
True
True

5.移除  strip 

strip:移除字符串左右两边的某些字符,只能是左右两边的

msg='   hello    '
n=msg.strip(' ')
print(n)   #移除空格
​
name_bak='egon'
pwd_bak='1234'
name=input('请输入你的姓名:').strip(' ')
pwd=input('请输入你的密码:').strip(' ')
if name==name_bak and pwd==pwd_bak:
    print('login successful')
else:
    print('输入的姓名或密码错误')
###移除左右两边
msg='*******+_+_+_0)hello****()*'
n=msg.strip('*')
print(n)
+_+_+_0)hello****()
#移除多个字符
msg='*******+_+_+_0)hello****()*'
n=msg.strip('*+_0)(')
print(n)
hello

6.切分 split

'字符串'.split  把有规律的字符串切成列表从而方便取值

msg='a;b;c;d;e;f;g;h;j'
n=msg.split(';',2)  #msg.split('',2)引号内表示以什么规律切分,数字2表示切分的列表内值的个数,以0计
print(n)
['a', 'b', 'c;d;e;f;g;h;j']
#若想将列表n重新变回字符串可用内置方法  
'字符'.join(列表)
s1=':'.join(n)    #就转换成字符串,:加在每个字符之间
print(s1)
a:b:c;d;e;f;g;h;j

7.循环

for n in 'hello':
    print(n)

需掌握的操作

1.strip   lstrip  rstrip

lstrip:表示从移除字符左边

rstrip:表示移除字符右边

msg='*****hello****'
print(msg.strip('*'))
print(msg.lstrip('*'))
print(msg.rstrip('*'))
hello
hello****
*****hello

2.lower   upper

msg = 'AaBbCc123123123'
print(msg.lower())#字符串小写
print(msg.upper())#字符串大写
aabbcc123123123
AABBCC123123123

3.startswith    endswith

判断字符串是否以'字符'开头   结尾

msg='alex is dsb'
print(msg.startswith('alex'))
print(msg.endswith('sb'))
True
True

4.format  

msg='my name is %s my age is %s' %('egon',18)
print(msg)
#通过%s引值   
msg='my name is {name} my age is {age}'.format(age=18,name='egon')
#利用format传值
print(msg)

5.split      rsplit

rsplit从右开始切分

cmd='get|a.txt|33333'
print(cmd.split('|',1))
print(cmd.rsplit('|',1))
['get', 'a.txt|33333']   #两种切分的结果很显然
['get|a.txt', '33333']

6.replace

msg='kevin is sb kevin kevin'
print(msg.replace('kevin','sb',2))
sb is sb sb kevin
#替换    语法msg.replace('旧的字符','新的字符',替换个数)

7.isdigit    当字符串为纯数字时显示True

res='11111'
print(res.isdigit())
int(res)
True
​
age_of_bk=18
inp_age=input('your age: ').strip()
if inp_age.isdigit():
    inp_age=int(inp_age) #int('asdfasdfadfasdf')
    if inp_age > 18:
        print('too big')
    elif inp_age < 18:
        print('to small')
    else:
        print('you got it')
else:
    print('必须输入纯数字')

猜你喜欢

转载自www.cnblogs.com/5j421/p/9998019.html