对整型、浮点型、字符串类型的认识

整型(int)
基本认识:
用途:记录年龄、等级、号码等
定义方式:age=18
类型总结:存一个值且为不可变类型
浮点型(float)
用途:记录身高、体重、薪资
定义方式:salary=3.1
类型总结:存一个值且为不可变类型
字符串类型(str)
一:基本使用

1 用途:记录描述性的状态,如人的名字,地址,性别

2 定义方式:在'',"",""""""内包含一系列的字符
msg='hello'#相当于msg=str('hello')
a1=str(1)
a2=str([1,2,3])
print(type(a1),type(a2))
优先掌握的操作:
1、按索引取值(正向取+反向取) :只能取
msg='hello world'
print(msg[4])
print(msg[-1])
2、切片(顾头不顾尾,步长)
msg='hello world'#就是从一个大的字符串中切除一个全新的子字符串
print(msg[0:5])
print(msg)
print(msg[::-1])
print(msg[0:5:2])
print(msg[5:0:-1])
print(msg[-1::-1])

3、长度len1
msg = 'hello world'
print(len(msg))
4、成员运算in和not in是为了判断一个子字符串是否在一个大字符串中
msg = 'hello world'
print('hello'in'hello world')
print('h'in'hello world')
print('h'not in'hello world')
5、移除空白strip是去掉字符串左右两边的字符strip,不管中间的
asd=' * liu - / + '
print(asd.strip())
print(asd.strip(' * - / +'))
age=input('>>').strip()
if age=='liu':
print('hello')
6、切分:针对按照某种分隔符组织的字符串,可以用split将其切分成列表,进而进行取值
msg='hello|world'
x=msg.split('|')
print(x)
s,d=msg.split('|')
msg.splitlines()#将字符串中内容全部放到列表中打印出来。
7、循环
msg='hello'
for i in msg:
print(i)
补充:1.strip,lstrip,rstrip
msg='****hello****'
print(msg.strip("*"))
print(msg.lstrip("*"))
print(msg.rstrip("*"))
2.lower,upper
msg='ADSxc'
print(msg.lower())
print(msg.upper())
3.startswith,endswith
msg='alex is dsb'
print(msg.startswith('alex'))
print(msg.endswith('d'))
print(msg.endswith('sb'))
4.format的三种玩法
print('my name is %s my age is %s' %('egon',18))
print('my name is {name} my age is {age}'.format(name='egon',age=18))
5.split,rsplit
print(('my |name|is').rsplit('|',1))
print(('my |name|is').split('|',1))
6.join
msg='my|name|is'
print(msg.rsplit("|"))
msg1='|'.join(msg.rsplit('|'))
print(msg)
7、replace
msg='alex say i have one tesla,alex is alex hahaha'
print(msg.replace('alex','sb'))
print(msg)
8、isdigit # 判断字符串中包含的是否为纯数字
print('10.1'.isdigit())
3 常用操作+内置的方法
二:该类型总结
1 只能存一个值且存的值是任意类型。


2 有序

3 不可变
不可变:值变,id就变。不可变==可hash
原文地址:https://www.cnblogs.com/ageliu/p/9360420.html