int() 代表传参,输出整型数字,相当于print(),input()的操作,但是print本身在系统内不产生数据
age = 10 # 实际上是age=int(10)
name=input('xxx')
res=print('xxx') # 没有产品
print(res) # 值为None
可以把纯数字的字符串转成int
了解1: 十进制转成其他进制
10进制 -> 二进制
11 - > 1011
1011-> 8+2+1
print(bin(11)) # 0b1011 (binary)
10进制 -> 八进制
print(oct(11)) # 0o13 (Octal)
10进制 -> 十六进制
print(hex(11)) # 0xb (Hexadecimal)
print(hex(123)) # 0xb
了解2: 其他制转成其十进制
二进制->10进制
print(int('0b1011',2)) # 11
二进制->8进制
print(int('0o13',8)) # 11
二进制->16进制
print(int('0xb',16)) # 11
使用时调用float功能
salary=3.1 # salary=float(3.1)
res=float("3.1")
print(res,type(res))
int与float的使用就是数学运算+比较运算
记录、描述性质和状态。
msg='hello' # msg=str('msg')
print(type(msg))
str可以把任意其他类型都转成字符串
>>> res=str({'a':1})
>>> print(res,type(res))
{'a': 1} <class 'str'>
msg='hello world'
正向取
print(msg[0]) # h
print(msg[5]) # " "
反向取
print(msg[-1]) # d
msg='hello world'
顾头不顾尾,从0取到4,不取5
>>>res=msg[0:5]
>>> print(res)
hello
>>> print(msg)
hello world
步长
res=msg[0:5:2] # 0 2 4
print(res) # hlo
反向步长(了解)
res=msg[5:0:-1]
print(res) #" olle"
msg='hello world'
res=msg[:] # res=msg[0:11] 默认起始位置为开始至最后
print(res) # hello world
res=msg[::-1] # 把字符串倒过来
print(res) # dlrow olleh
msg='hello world'
print(len(msg)) # 字符长度11
判断一个子字符串是否存在于一个大字符串中
print("alex" in "alex is sb")
print("alex" not in "alex is sb")
print(not "alex" in "alex is sb") # 不推荐使用
默认去掉的空格
msg=' egon '
res=msg.strip()
print(msg) # 不会改变原值
print(res) # 是产生了新值,去掉了左右的空格
msg='****egon****'
print(msg.strip('*')) # egon
了解:strip只去两边,不去中间
msg='****e*****gon****'
print(msg.strip('*')) # e*****gon
msg='**/*=-**egon**-=()**'
print(msg.strip('*/-=()')) # egon
应用:
inp_user=input('your name>>: ').strip() # inp_user=" egon" 防止错输入空格
inp_pwd=input('your password>>: ').strip()
if inp_user == 'egon' and inp_pwd == '123':
print('登录成功')
else:
print('账号密码错误')
默认分隔符是空格
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)
info='egon:18:male'
for x in info:
print(x)
msg=‘egon‘
print(msg.strip(‘‘))
print(msg.lstrip(‘‘))
print(msg.rstrip(‘‘))
msg=‘AbbbCCCC‘
print(msg.lower())
print(msg.upper())
print("alex is sb".startswith("alex"))
print("alex is sb".endswith(‘sb‘))
info="egon:18:male"
print(info.split(‘:‘,1)) # ["egon","18:male"]
print(info.rsplit(‘:‘,1)) # ["egon:18","male"]
l=[‘egon‘, ‘18‘, ‘male‘]
res=l[0]+":"+l[1]+":"+l[2]
res=":".join(l) # 按照某个分隔符号,把元素全为字符串的列表拼接成一个大字符串
print(res)
l=[1,"2",‘aaa‘]
":".join(l)
msg="you can you up no can no bb"
print(msg.replace("you","YOU",))
print(msg.replace("you","YOU",1))
判断字符串是否由纯数字组成
print(‘123‘.isdigit())
print(‘12.3‘.isdigit())
age=input(‘请输入你的年龄:‘).strip()
if age.isdigit():
age=int(age) # int("abbab")
if age > 18:
print(‘猜大了‘)
elif age < 18:
print(‘猜小了‘)
else:
print(‘猜对了‘)
else:
print(‘请输入数字‘)
原文:https://www.cnblogs.com/zuiyouyingde/p/12459520.html