字符串类型的内置方法
- 用途:描述性质的东西,如名字,地址,国家等
- 定义:用单双引号或三引号包裹的字符,str() 方法可以把其他数据类型强制转换为字符串类型
name = 'nick' # name =str('nick') s1 = str(1.1) s2 = str([1, 2, 3]) print(f's1:{s1}, type:{type(s1)}') print(f's2:{s2}, type:{type(s2)}') s1:1.1, type:<class 'str'> s2:[1, 2, 3], type:<class 'str'>
- 常用操作和内置方法
1.按索引取值
msg='hello world' # 0123456789... #索引序号 print(msg[4]) ===> o print(msg[-3]) ===> r
2.切片(顾头不顾尾,步长为正从左到右,步长为负从右到左)
msg='hello world' print(msg[3:7]) ===>'lo w' print(msg[3:]) ===>'lo world' print(msg[3::2]) ===>'l ol' #从3开始到最后步长为2取值 print(msg[:]) ===>'hello world' #从头到尾 print(msg[::-1]) ===>'dlrow olleh' #反转所有 print(msg[-5:-2]) ===>'wor' print(msg[-2:-5:-1]) ===>'lro'
3.长度len
msg='hello world' print(len(msg)) ===>11
4.成员运算 in 和 not in
msg='hello world' print('hello' in msg) ===>True print('hi' in msg) ===>False print(not 'hello' in msg) ===>False
5.移除字符串两端空白或指定字符用 strip(),lstrip()和rstrip()只移除左边或右边空白和指定字符
msg=' hello world ' print(msg.strip()) ===>'hello world' print(msg.strip(' hld')) ===>'ello wor' print(msg.rstrip(' hld')) ===>' hello wor' print(msg.lstrip(' hld')) ===>'ello world '
6.切分 split()和rsplit(),split()从左边开始切分,rsplit()从右边开始切分
info = 'nick:male:19' info_list1 = info.split(':') info_list2 = info.split(':', 1) print(info_list1) print(info_list2) ['nick', 'male', '19'] ['nick', 'male:19'] info='nick:male:19' print(info.rstrip(':',1)) ['nick:male',19]
7.循环
msg = 'hello nick' for i in msg: print(i) h e l l o n i c k
8.大小写lower()和upper()
lower():字符串的字母全部变成小写
upper():字符串的字母全部变成大写
name='Alex' print(name.lower()) print(name.upper()) 'alex' 'ALEX'
9.startswith()和endswith(),判断字符串以什么开始或者结束,返回True或者False
s='hello world' print(s.startswith('hell')) print(s.endswith('ld')) True True
10.join字符串拼接(数字不可以和字符串拼接)
lis=['hello','world','i','love','you'] lis_new=' '.join(lis) print(lis_new) 'hello world i love you'
11.替换 replace()
s='hello world i love you' s_new=s.replace('world','China') print(s_new) 'hello China i love you'
12.isdigit()判断字符串是否是纯数字,是纯数字返回True,否则返回False
s='110' print(s.isdigit()) #True s='11.0' print(s.isdigit()) #False
其他操作(了解)
1.find() | rfind() | index() | count()
msg='my name is jack,jack is me' print(msg.find('jack')) #11 返回目标第一个字符的索引,找不到返回-1 print(msg.rfind('jack')) #16 从右边开始寻找,返回目标第一个字符的索引,找不到返回-1 print(msg.index('jack')) #11 找不到会报错 print(msg.count('jack')) #2
2.center() | ljust() | rjust() | zfill()
name='jack' print(name.center(20,'*')) #********jack******** print(name.ljust(20,'*')) #jack**************** print(name.rjust(20,'*')) #****************jack print(name.zfill(20)) #0000000000000000jack 默认用0填充
3.captalize() ,swapcase() ,title()
name='jAck' print(name.captalize()) #Jack 第一个字符大写,其他的小写 print(name.swapcase()) #JaCK 小写变大写,大写变小写 print(name.title()) #每个单词首字母大写
4.expandtabs()
print("a\\tb\\tc: %s"%('a\tb\tc\t')) # 默认制表符8个空格 print("'a\\tb\\tc'.expandtabs(32): %s"%('a\tb\tc\t'.expandtabs(16))) a\tb\tc: a b c 'a\tb\tc'.expandtabs(32): a b c
- 存一个or多个值:一个值
- 有序or无序:有索引的都是有序的,因此字符串是有序的
- 可变or不可变:不可变类型
原文地址:https://www.cnblogs.com/zhaogang0104/p/10914399.html
时间: 2024-10-03 08:18:25