2018.09.17python学习第五天part2

 2.str
# msg="hello world"
# print(type(msg)) #可以发现类型为str
# print(msg[1]) # e
# 所以可以像列表一样按索引取值,所以str是有序
#
# 常用的操作:
# 1.按索引取值(正向取+反向取) ***只能取
# msg="hello world"
# print(msg[])#中括号内填对应的位数就取出了对应的值
# print(msg[len(msg)-1])#用len(msg)的方法取最后一个值
# print(msg[-1])#用list的方式直接取最后一个值

# 2.切片#和取大同小异,取多个值而已
# print(msg[0:5:1])# hello
# msg[a:b:c]
# a:其实位数(最头上开始可以省略)
# b:结束位数(最末尾结束可以省略)
# c:步长(不写默认为1,反向取步长为负数)
# 3.长度len
# msg="hello world"
# print (len(msg))#11
# 4.成员运算in or not int
# msg="hello world"
# print("hello" in msg)#True
# 5.移除空白strip
# msg=" hello "
# msg1=msg.strip()
# print(msg1)#会发现空格没有了
# 同理:
# msg="#$%^&hello*&^%"
# print(msg.strip(#$%^&))#hello
# 可以用在登陆系统是用户不小心多输入了空格
# name=input("please input your name: ").strip()
# pwd=input("please input your password: ").strip()
# 扩展:
# lstrip:向左移除
# rstrip:向右移除
# strip:两边移除是一样的道理
# 6.切分split#针对有规律的字符串按照某个字符切成列表
# msg="egon:18:female:music"
# msg1=msg.split(":")
# print(msg1)
# split("切割符",切割次数)
# 拓展:
# rsplit:从右往左切
# split:是从左往右切
# 7.循环
# msg="hello"
# for item in msg
# print(item)#
# 8.lower/upper(改小写/改大写)
# msg="hello"
# msg1=msg.upper()
# print(msg1)
# 9.startswith/endswith(以什么开始/以什么结束)
# msg="hello"
# print(msg.startswith("h"))#True
# print(msg.endswith("o"))#True
# 10.format的三种玩法:
# print("my name is %s my age is %s" %("tony",18))
# print("my name is{name} my age is {age}".format(age=18,name="tony"))
# print("my name is{0} my age is {1}".format(18,"tony"))
# print("my name is{1} my age is {0}".format(18,"tony"))
# 11.join#用什么连接成一个字符串
# msg='a:b:c:d:e'
# L=msg.split(':')
# L1="*".join(L)
# print(L1)
# # 12.replace#取代
# msg="egon is egon and agon is"
# msg1=msg.replace("egon","sb",1)
# print(msg1)
# 13.isdigit#是否是纯数字的字符串
# print("123".isdigit())#True
# 比如用在猜年龄的游戏中:
# age_of_bd=30
# input_age=input(">>>: ").strip()
# if input_age.isdigit():
# pass
#
str类型总结:
1.存一个值
2.有序
3.不可变

posted on 2018-09-17 16:59  撩与诗人  阅读(192)  评论(0编辑  收藏  举报