一:基本使用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、长度len1msg = ‘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(‘|‘)7、循环msg=‘hello‘for i in msg: print(i)补充:1.strip,lstrip,rstripmsg=‘****hello****‘print(msg.strip("*"))print(msg.lstrip("*"))print(msg.rstrip("*"))2.lower,uppermsg=‘ADSxc‘print(msg.lower())print(msg.upper())3.startswith,endswithmsg=‘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,rsplitprint((‘my |name|is‘).rsplit(‘|‘,1))print((‘my |name|is‘).split(‘|‘,1))6.joinmsg=‘my|name|is‘print(msg.rsplit("|"))msg1=‘|‘.join(msg.rsplit(‘|‘))print(msg)7、replacemsg=‘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 可变or不可变
不可变:值变,id就变。不可变==可hash
原文地址:https://www.cnblogs.com/ageliu/p/9360420.html
时间: 2024-10-12 06:53:00