Python编程整理:字符串中常用的操作

Python编程整理:字符串中常用的操作

1. 切片

msg[(起始位置):(结束位置):(步长)]

顾头不顾尾

步长默认为1,并且可以是负数,表示反向取

例子:

msg = "hello world"
print(msg[0:2])
# he
msg = "hello world"
print(msg[::-1])
# dlrow olleh

[::-1]为常用的逆序输出

2. 成员运算in以及not in

扫描二维码关注公众号,回复: 11455015 查看本文章
msg = "a b c d e f"
print('a' in msg)
# True
msg = "a b c d e f"
print('a' not in msg)
# False

3. 移除字符串左右两边的字符strip

移除空格的用法:

msg = '     content    '
print(msg.strip())
# content

移除两边的字符:

msg = '   **!  content   !** '
print(msg.strip('*! '))
# content

在strip中写入所有想去除的字符,字符串两段的这些字符就会被去除

另外,还有去除单边字符的函数lstrip和rstrip,例子:

msg = '     content    '
print(msg.lstrip())
print(msg.rstrip())
# content    
#      content

4. 替换字符函数replace

msg = 'abababab'
print(msg.replace('ab', 'AB'))
# ABABABAB

将给定的字符替换为其他字符。

同时,replace还能加第三个参数来规定替换的次数,比如:

msg = 'abababab'
print(msg.replace('ab', 'AB', 2))
# ABABabab

5. 切分操作split

msg = '1|2|3|4'
list1 = msg.split('|')
print(list1)

# ['1', '2', '3', '4']

split中还可以添加一个正整数参数,代表切分次数,比如:

msg = '1|2|3|4'
list1 = msg.split('|', 2)
print(list1)

# ['1', '2', '3|4']

另外,rsplit函数时从右往左切割,在不指定次数的情况下,结果个split相同。

msg = '1|2|3|4'
list1 = msg.rsplit('|', 2)
print(list1)

# ['1|2', '3', '4']

 6.连接操作join

可以理解为split的逆向操作

list1 = ['1', '2', '3', '5']
print(':'.join(list1))

# 1:2:3:5

这边要注意的是,列表中的元素必须全部都是字符串类型才能使用join函数来连接

7.判断字符串是否为纯数字isdigit

msg1 = 'abc123'
msg2 = '123456'

print(msg1.isdigit())
print(msg2.isdigit())

# False
# True

8.格式化字符输出.format方法(最常用)

a = 'hello'
b = 'world'

print('{} {}'.format(a ,b))

# hello world

重复字符使用如下:

a = 'hello'
b = 'world'

print('{0} {0} {0} {1} {1}'.format(a, b))

# hello hello hello world world

也可以用临时变量来实现重复:

a = 'hello'
b = 'world'

print('{text1} {text1} {text2} {text2} {text2}'.format(text1 = a, text2 = b))

# hello hello world world world

还有个简便用法,只限定python3.5之后:(暂时不建议使用)

a = 'hello'
b = 'world'

print(f'{a} {b} {b}')

# hello world world

  

9. 寻找操作 find

msg = 'hello world'
print(msg.find(' '))

# 5

空格第一次出现在第5个字符,因此输出的是数字5

rfind函数则是从左往右找,例如:

msg = 'hello world'
print(msg.find('l'))
print(msg.rfind('l'))

# 2
# 9

find找到的‘l’字符是在第2个位置上的

rfind找到的‘l’字符实在第9个位置上的

找不到会返回-1:

msg = 'hello world'
print(msg.find('a'))

# -1

find还能添加两个参数分别表示起始位置和结束位置:

msg = 'hello world'
print(msg.find('l', 7, 10))

# 9

count函数统计出现字符的次数:

msg = 'hello world'
print(msg.count('l'))

# 3

10. 文本打印操作

center居中操作:

msg = 'hello world'
print(msg.center(50,'*'))

# *******************hello world********************

center第一个参数为宽度设置,第二个字符参数为填充字符

ljust居左显示与rjust居右显示:

msg = 'hello world'
print(msg.ljust(50, '*'))
print(msg.rjust(50, '*'))

# hello world***************************************
# ***************************************hello world

零填充函数zfill,默认从右对齐,在规定长度下用0填充,比如:

num = '111'
print(num.zfill(10))

# 0000000111

11. 制表符操作

expandtabs可以改变指标符\t的空格数量

比如:

print('hello\tworld')
print('hello\tworld'.expandtabs(1))

# hello    world
# hello world

制表符默认空格数为4

12.英文字母大小写操作

msg = 'hello world'

print(msg.upper())
print(msg.lower())
print(msg.capitalize())
print(msg.swapcase())  # 大写变小写 小写变大写
print(msg.title())

# HELLO WORLD
# hello world
# Hello world
# HELLO WORLD
# Hello World

13.识别数字

isdigit可以识别纯数字,而isnumeric函数可以识别数字相关的文字,比如:

num = ''
print(num.isnumeric())

# True

猜你喜欢

转载自www.cnblogs.com/cutomorrowsmile/p/13383607.html