python之数字类型、字符串类型

一、 数字类型
1.int类型
作用:
定义:

age = 10 # age = int(10)

类型转换:
2.1 纯数字的字符串转成int

res = int('100011')
print(res,type(res))

2.2(了解)
2.2.1 十进制转换成其他进制
10进制—> 二进制

print(bin(11))

10进制–>八进制

print(oct(11))

10进制–>十六进制

print(hex(11))

2.2.2其他进制转成十进制
二进制–>十进制

print(int('0b1011',2)

二进制–>八进制

print(int('0o13',8))

二进制–>十六进制

print(int('0xb',16))

使用
2.float类型
作用
定义

salary = 3.1 #salary = float(3.1)

类型转换

res = float('3.1')
print(res,type(res))

使用
int与float没有需要掌握的内置方法,他们的使用就是数学运算+比较运算

二、字符串类型
1.作用
2.定义

msg = ‘hello’
print(msg,type(msg))

3.类型转换
str可以把任何其他类型转成字符串

res = str({
    
    'a':1})
print(res,type(res))

4.使用:内置方法
4.1优先掌握
4.1.1 按索引取值(正向取+反向取):只能取

```css
msg = 'hello world'
#正向取
print(msg[0])
print(msg[5])
#反向取
print(msg[-1])
#只能取
msg[0] = 'H'  #报错
'''

4.1.2 切片(顾头不顾尾,步长) 索引的拓展应用,从一个大的字符串中拷贝出一个子字符串

msg = ‘hello world’
res = msg[0:5]
print(res)
print(msg)

#步长
res = [0:5:2]
print(res)
#反向步长(了解)
res =  msg[5:0:-1]
print(res)
res = msg[:] # res = msg[0:11]
print(res)
res = msg[::-1] #把字符串倒过来
print(res)

4.1.3 长度 len

print(len(msg))

4.1.4 成员运算in 和 not in
判断一个子字符串是否在一个大字符串中

print('alex' not in 'alex is sb')
print('alex' in 'alex is sb')

4.1.5 移除字符串左右两侧的符号

msg = ‘           egon       ’
res = msg.strip() #默认去掉的是空格
print(res)
print(msg)

msg = '********egon**********'
res = msg.strip('*')
了解:strip只去两边,不去中间
msg = ‘**********ego***n*****’
res = msg.strip('*')

msg = '^&*egon^&*'
res = msg.strip('^&*)
print(res)
#应用
inp_user = input('your name: ').strip()
inp_pwd = input('your password:').strip()
if inp_user == 'egon' and inp_pwd == '123':
	print('登陆成功')
else:
	print('账号或密码错误')
	

4.1.6 切分split:把一个字符串按照某种分隔符进行切分,得到一个列表

info = 'egon 18 male'
res = info.split() #默认分隔符是空格
print(res)

info = 'egon:18:male'
res = info.split(':')
print(res)
#指定分割次数(了解)
info = 'egon:18:male'
res = info.split(':',1)
print(res)

4.1.7 循环

info = 'egon :18:male'
for x in info:
	print(x)

4.2 需要掌握
4.2.1 strip、lstrip、rstrip

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

4.2.2 lower,upper

 msg = 'AbbbbCCC'
 print(msg.lower())
 print(msg.upper())

4.2.3 startswith,endwith

 print('alex is sb'.startswith('alex'))
 print('alex is sb'.endswith('sb'))

4.2.4 format的三种玩法
4.2.5 split,rsplit: 将字符串切成列表

info = 'egon:18:male'
 print(info.split(':',1))
 print(info.rsplit(':',1))

4.2.6 join :把列表拼接成字符串

l = ['egon','18','male']
res = l[0]+l[1]+l[2]
print(res)

res = ':'.join(l) # 按照某个分隔符号,把元素全为字符串的列表拼接成一个大的字符串
print(res)

4.2.7 replace

 msg = 'you can you up no can no bb'
 res = msg.replace('you','YOU')
 res = msg.replace('you','YOU',1)
 print(res)

4.2.8 isdigit
判断字符串你是否由数字组成

 print('123'.isdigit())
 print('12.3'.isdigit())

 age = input('请输入你的年龄:').strip()
 if age.isdigit():
     age = int(age)
     if age > 18:
         print('猜大了')
     elif age < 18:
         print('猜小了')
     else:
         print('猜对了')
 else:
     print('必须输入数字,傻子')

4.3了解
4.3.1 find、rfind、index、rindex、count

msg = 'hello egon ahahah'
 print(msg.find('e')) # 返回要查找的字符串在大字符串中的起始索引
 print(msg.index('e'))
 # print(msg.index('xxx')) # 抛出异常
 print(msg.find('xxx')) # 返回-1,代表找不到

 print(msg.count('egon'))

4.3.2 cennter、ljust、rjust、zfill

 print('egon'.center(50,'*'))
 print('egon'.ljust(50,'*'))
 print('egon'.rjust(50,'*'))
 print('egon'.zfill(10))

4.3.3 expandtabs

 msg = 'hello\tworld'
 print(msg.expandtabs(2)) # 设置制表符代表的空格数为2

4.3.4 captalize、swapcase、title

 print('hello world egon'.capitalize())
 print('hello world egon'.title())
 print('Hello WorLd egon'.swapcase())

4.3.5 is数字系列
4.3.6 is其他

print('abc'.islower())
 print('ABC'.isupper())
 print('Hello World'.istitle())
 print('123add'.isalnum()) # 字符串由字母或数字组成结果为True
 print('abc'.isalpha())  # 字符串由字母组成结果为True
 print('  '.isspace())  # 字符串由空格组成结果为True
 print('age_of_egon'.isidentifier())
 print('if'.isidentifier())


num1 = b'4' # bytes
num2 = u'4' # unicode\python3中无需加u就是unicode
num3 = '四' # 中文数字
num4 = 'IV' # 罗马数字

 isdigit只能识别:前两者
 print(num1.isdigit())
 print(num2.isdigit())
 print(num3.isdigit())
 print(num4.isdigit())

 isnumberic

 print(num2.isnumberic()) # True
 print(num3.isnumberic()) # True
 print(num4.isnumberic())  # True

 isdecimal
print(num2.isdecimal()) # True

猜你喜欢

转载自blog.csdn.net/weixin_47237915/article/details/114294320