2-07字符串类型讲解
数据类型-字符串
字符串是一个有序的字符集合,用于存储和表示基本的文本信息,一对单、双或三引号中间包含的内容称之为字符串。
创建:
s = 'Hello,beauty!How are you?'
特性:
- 有序
- 不可变
常用的: isdigit replace find count index split strip center format join
s = 'Hello World!' #大写转小写,小写转大写 print(s.swapcase()) #首字母大写 print(s.capitalize()) #全部变成小写 print(s.casefold()) # 把字符串放在指定符号中间:*******************Hello World!******************* print(s.center(50,'*')) #统计某个字符数 print(s.count('o')) #2 print(s.count('o',0,5)) #指定统计范围 1 #判断字符串是否以某个字符结尾 print(s.endswith('!')) # True #扩展tab键,把tab键的默认长度变长 s1 = 'a\tb' print(s1) # a b print(s1.expandtabs(20)) # a b #查找某个字符,返回索引 print(s.find('W')) #6 print(s.find('W',0,3)) #可指定查找范围 没找到返回-1 #字符串格式化 s3 = 'my name is {0},i am {1} years old.' print(s3.format('Alex',22)) # my name is Alex,i am 22 years old. s3 = 'My name is {name}, I am {age} years old.' print(s3.format(name='Alex',age = '22'))
>>> #返回指定字符的索引 ... >>> s = 'Hello World!' >>> s.index('o') 4 >>> s.index('o',5,6) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: substring not found
>>> #是不是数字和字母 ... >>> '22b'.isalnum() True >>> '22b*'.isalnum() False
>>> #是不是字母 ... >>> '222'.isalpha() False >>> 'abcc'.isalpha() True >>> '22bb'.isalpha() False
>>> #是不是整数 ... >>> '234'.isdecimal() True >>> '2ab4'.isdecimal() False >>> '22.22'.isdecimal() False >>>
>>> #是否是可用的变量名 ... >>> '332'.isidentifier() False >>> 'd332'.isidentifier() True >>>
>>> #判断是不是小写 ... >>> 'ab'.islower() True >>> 'ab3'.islower() True >>> 'abAA'.islower() False
>>> #判断字符串中是不是只有数字 ... >>> 'ab4'.isnumeric() False >>> '114'.isnumeric() True >>>
>>> #判断是否可打印 ... >>> '2bg'.isprintable() True
>>> #判断是不是空格 ... >>> ' '.isspace() True >>> 'ab'.isspace() False >>> 'a b'.isspace() False
>>> #是不是大写 ... >>> 'ABC'.isupper() True >>> >>> 'abA'.isupper() False
>>> #把列表变成字符串 ... >>> names = ['alex','jack','rain'] >>> ''.join(names) 'alexjackrain' >>> ' '.join(names) 'alex jack rain' >>> '*'.join(names) 'alex*jack*rain' >>>
>>> #把字符串长度扩从,用指定字符或者空格 ... >>> s = 'Hello World!' >>> s.ljust(50) 'Hello World! ' >>> s.ljust(50,'-') 'Hello World!--------------------------------------'
>>> #把字母变成小写 ... >>> s = 'Hello World!' >>> s.lower() 'hello world!' >>> #把字母变成大写 ... >>> s.upper() 'HELLO WORLD!'
>>> #去掉两边的空格、换行、tab键等 ... >>> s = '\n hello world ' >>> s.lstrip() 'hello world ' >>> s.rstrip() '\n hello world'
>>> #类似密码表的映射关系
>>> str_in = 'abcdef' >>> str_out = '!@#$%^' >>> table=str.maketrans(str_in,str_out) >>> 'abc'.translate(table) '!@#'
>>> #替换 >>> s.replace('o','-') 'Hell- W-rld' >>> s.replace('o','-',1) 'Hell- World'
>>> #查找返回位置 ... >>> s = 'hello world!' >>> s.rfind('o') 7 >>> s.rindex('o') 7
>>> #把字符串转成列表 ... >>> s = 'hello world' >>> s.split() ['hello', 'world'] >>> s.split('o') ['hell', ' w', 'rld'] >>> s.split('o',1) ['hell', ' world'] >>> s.rsplit('o',1) ['hello w', 'rld']
>>> #按行把字符串转成列表 ... >>> s = 'a\nb\nc\ndld\n' >>> s.splitlines() ['a', 'b', 'c', 'dld']
>>> #判断字符串的开头 ... >>> s = 'hello world' >>> s.startswith('he') True >>> s.startswith('hello') True >>> s.startswith('heh') False
>>> s.endswith('d') True
>>> #把字符串变成指定长度,不够的用0补齐 ... >>> s = 'hello world' >>> s.zfill(40) '00000000000000000000000000000hello world'