文章目录
- 字符串的基本操作
- 1.大小写互换:swapcase
- 2.只有首字母大写:capitalize
- 3.大小写统一小写:casefold
- 4.居中显示:==center==
- 5.从一个范围内统计某str出现的次数:count
- 6.判断是否以某str结尾: endswith
- 7.查找返回索引值:find,找不到返回-1
- 8.字符串格式化:==format==
- 9.返回索引:==index==,找不到报错
- 10.Python中的isdigit() isdigit() isdigit()的区别
- 11. 去除换行/空格/tab键或指定的元素:==strip==
- 12.替换:==replace==
- 13.连接序列中的元素:==join==
- 14.通过分隔符拆分序列:split
- 15.全部小写:lower,全部大写:upper,每个独立单词首字母大写:title
- 16.translate 和 maketrans
- 输入层数,利用center,format方法生成星号塔
字符串是一个 有序的字符的集合,用于存储和表示基本的文本信息.
字符串的基本操作
所有的标准序列的操作(索引/切片/长度等)对字符串同样适用
可以在按住CTRL键的时候点击相应的操作命令,弹出英文详细解释。
s =‘Hello,World’
1.大小写互换:swapcase
print(s.swapcase())
hELLO,wORLD
2.只有首字母大写:capitalize
print(s.capitalize())
Hello,world
3.大小写统一小写:casefold
print(s.casefold())
hello,world
4.居中显示:center
print(s.center(15))
Hello,World #15位,左右不够默认补充空格
print(s.center(15,'*'))
**Hello,World**
5.从一个范围内统计某str出现的次数:count
print(s.count('l'))
3
print(s.count('l',0,4))
2
6.判断是否以某str结尾: endswith
print(s.endswith('d'))
True
print(s.endswith('d',0,4))
False
7.查找返回索引值:find,找不到返回-1
如果找到,就返回子串的第一个字符的索引,否则返回-1。
print(s.find('o',5,8))
7
print(s.find('o',5,7))
-1 #不存在返回-1,由此看位置含左不含右
可指定搜索的起点和终点包含起点,但不包含终点
8.字符串格式化:format
9.返回索引:index,找不到报错
print(s.index('o',7,8))
7
print(s.index('o',7,7))
ValueError: substring not found #找不到报错
10.Python中的isdigit() isdigit() isdigit()的区别
https://blog.csdn.net/Com_ma/article/details/77539833
11. 去除换行/空格/tab键或指定的元素:strip
strip将字符串开头和末尾的空白(但不包括中间的空白)删除,并返回删除后的结果。
s=' he llo, wor l d '
print(s.strip()) #左右两边的
he llo, wor l d
s='%**** hello,world**** % '
print(s.strip('%* '))#制定元素空格全部去除
hello,world
12.替换:replace
s=' he llo, wor l d '
print(s.replace(' ','')) #替换空格为空
hello,world
s='this is bike'
print(s.replace('bike','car'))
this is car
13.连接序列中的元素:join
list=['1','2','3','4','5'] #元素必须为字符串,不然报错
s='*'
print(s.join(list))
1*2*3*4*5
14.通过分隔符拆分序列:split
join的逆方法
s='I love python'
l= s.split(' ')
print(l)
['I', 'love', 'python']
s='hello,world'
l=s.split('o')
print(l)
['hell', ',w', 'rld']
l=s.split('o',1) #拆分一次
print(l)
['hell', ',world']#从左往右拆
l=s.rsplit('o',1)#从右边开始
print(l)
['hello,w', 'rld']
15.全部小写:lower,全部大写:upper,每个独立单词首字母大写:title
s='Hello,WORLD'
print(s.lower())
print(s.upper())
print(s.title())
hello,world
HELLO,WORLD
Hello,World
16.translate 和 maketrans
方法translate与replace一样替换字符串的特定部分,但不同的是它只能进行单字符替换。这个方法的优势在于能够同时替换多个字符,因此效率比replace高。
s='I love python, I like mike, alike bike, abs'
table = s.maketrans('pyh','*l%') #制作一个替换表,长度相等,不然报错。'pyh'对应'*l%'
print(s.translate(table)) #根据table转换s
I love *lt%on, I like mike, alike bike, abs
添加第三个参数值,表示要删除的字符,注意不是字符串,如果参顺值为’ab‘,转换时会删除’a‘,’b’和’ab’,如下:
table = s.maketrans('pyh','*l%','ab')
print(s.translate(table))
I love *lt%on, I like mike, like ike, s
输入层数,利用center,format方法生成星号塔
1、center
floor=int(input('层数:'))
s='*'
n=2*floor-1
i = 1
while i <= floor:
l1=s*(2*i-1)
print(l1.center(n)) #居中对齐,层数是8,那每一行都是15位。
i += 1
*
***
*****
*******
*********
***********
*************
***************
2、format
floor = int(input("请输入层数:"))
i=1
while i < floor:
print('{:>{a}}'.format('*'*(2*i-1),a=floor+i-1)) #右对齐,减少内存空间,每一行位数递增。
i += 1
*
***
*****
*******
*********
***********
*************