关于python3.x:Python3常用字符串操作

13次阅读

共计 2807 个字符,预计需要花费 8 分钟才能阅读完成。

1. str.strip 去掉字符串首位指定的字符, 默认去掉空白字符

intput="nnn 入门小站 nnn"
print('[%s]' % input)
# strip 不会扭转原字符串的值, 所以须要一个新变量接管
newInput=input.strip()
print('[%s]' % newInput)
# 去掉首位的 n 字符
print('[%s]' % input.strip('n'))
# 去掉首位的 n 字符 + 空白字符
print('[%s]' % input.strip('n').strip())
[入门小站]
[入门小站]
[入门小站]
[入门小站]

2. str.center 应用指定字符对字符串进行对齐

intput="入门小站"
print(input.center(12,'#'))
### 入门小站 ###

3. str.count() 统计字符串再另外一个字符串中呈现的次数

# 语法 str.count(value, start, end) 
# start 起始索引 可选
# end  完结索引  可选
input="入门 入门小站 入门 rumen 入门"
print(input.count('入门'))
# 指定查找范畴
print(input.count('入门',2,7))
4
1

4. str.find() 查找字符串首次呈现的地位

# 语法 str.find(value,start,end)
# start 起始索引 可选
# end  完结索引  可选
# 如果没有找到则返回 -1
input="入门 入门小站 入门 rumen 入门"
print(input.find('入门'))
# 指定范畴
print(input.find('入门',2,7))
0
3

5. str.rfind(value,start,end) 查找字符串最初呈现的地位

# 语法 str.rfind(value,start,end)
# start 起始索引 可选
# end  完结索引  可选
# 如果没有找到则返回 -1
input="入门 入门小站 入门 rumen 入门"
print(input.rfind('入门'))
# 指定范畴
print(input.rfind('入门',2,7))
17
3

6. str.swapcase() 返回一个字符串的正本, 并且对字符串进行大小写转换

input="ru men XIAO zhan"
print(input.swapcase())
 RU MEN xiao ZHAN 

7. str.startswith() 判断字符串是不是以某个字符串开始

# 语法:str.startswith(prefix[, start[, end]]) -> bool
# start 起始索引 可选
# end  完结索引  可选
# 返回 bool 值

input="入门小站"
print(input.startswith('入门'))
# 指定范畴
print(input.startswith('入门',2))
True
False

8.str.endswith() 判断字符串是不是以某个字符串结尾

# str.endswith(suffix[, start[, end]]) -> bool
# start 起始索引 可选
# end  完结索引  可选
# 返回 bool 值

input="入门小站"
print(input.endswith('小站'))
# 指定范畴
print(input.endswith('小站',2))
True
True

9. str.split() 宰割字符串成一个 list, 默认宰割符是任意数量的空白字符

# 语法:str.split(self, /, sep=None, maxsplit=-1)
input="n 入门  n  小站 n"
print(input.split())
# 指定宰割字符
print(input.split('n'))
['n', '入门', 'n', '小站', 'n']
['',' 入门  ','  小站 ',' ']

10. 字符串大小写转换

# 10.1 str.capitalize() 将字符串的第一个字符转成大写
input="ru mEn z"
print(input.capitalize())
# 10.2 str.upper() 将所有字符转换成大写
print(input.upper())
# 10.3 str.title() 将每个单词的首字符转换成大写, 其余字符转换成小写
print(input.title())
Ru men z 
RU MEN Z 
Ru Men Z 

11. str.ljust() 和 str.rjust() 字符串首位用指定的字符填充到指定长度,默认以空格填充

# 语法:ljust(self, width, fillchar=' ', /)
# 语法:rjust(self, width, fillchar=' ', /)
# width 为填充后的字符串的长度
input="rumenz"

print(intput.ljust(20,'#'))
print(input.rjust(20,'$'))
 入门小站 ################
$$$$$$$$$$$$$$rumenz

12. str.zfill() 字符串后面填充 0

# 语法:str.zfill(self, width, /)
# width 字符串填充后字符串的长度
input="rumenz"
print(intput.zfill(20))
0000000000000000 入门小站

13. 定义一个多行字符串

input='''
入门小站
rumenz

'''
print(input)
 入门小站
rumenz

14. 判断字符串是否在另外一个字符串中

ints="ru men z"
print('one' in ints)
False

15. is 判断字符串地址时候雷同

str1="rumenz"
str2="rumenz"
print(str1 is str2)
True

16. 判断字符串的类型

# str.isalnum() 判断字符串是不是由字母和数字组成
str1="rumenz123"
print(str1.isalnum())
# str.isalpha() 判断字符串是不是由字母组成
print(str1.isalpha())
# str.isdigit() 判断字符串是不是纯数字
print(str1.isdigit())
True
False
False

17. 数组拼接成字符串

# 语法 "".join(str)
li=['入门','小站','123']
print(''.join(li))
print('-'.join(li))
 入门小站 123
入门 - 小站 -123

18. 字符串替换

# 语法 str.replace(self, old, new, count=-1, /)
# old 旧字符串
# new 要替换成的新字符串
# count 替换的次数,默认为 -1 , 全副替换
str1="入门小站入门"
print(str1.replace("入门","rumen"))

rumen 小站 rumen

19. 字符串映射

# a->1 b->2 c->3 d->4 e->5 f->6
p=str.maketrans('abcdef','123456')
print('aaacccd'.translate(p))
1113334


正文完
 0