Python学习————字符串基本操作

字符串相关操作:

strip:

Python strip() 方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列。

注意:该方法只能删除开头或是结尾的字符,不能删除中间部分的字符。

name = '*dimple**'
print(name.strip('*'))  # 移除字符串指定字符
print(name.lstrip('*'))  # 移除字符串头部指定字符
print(name.rstrip('*'))  # 移除字符串尾部指定字符


# dimple
# dimple**
# *dimple

lower,upper:

lower() 方法用来把大写字符转变成小写字符
upper() 方法用来把小写字符转变成大写字符

name = 'dimple_Y'
print(name.lower())	 小写
print(name.upper())	 大写

# dimple_y
# DIMPLE_Y

startswith,endswith:

startswith() 语法:
str.startswith(str, beg=0,end=len(string));

startswith() 方法用于检查字符串是否是以指定子字符串开头,
如果是则返回 True,否则返回 False。
如果参数 beg 和 end 指定值,则在指定范围内检查。

endswuth()语法:
str.endswith(suffix[, start[, end]])

endswuth() 方法用于检查字符串是否是以指定字符串结尾,
可选参数"start"与"end"为检索字符串的开始与结束位置。

name = 'dimple_Y'
print(name.startswith('dimple'))
print(name.endswith('Y'))

# True
# True

format的三种用法:

res1 = '{} {} {}'.format('dimple', 18, '男')  # 不设置指定位置,按默认顺序
res2 = '{0} {1} {2}'.format('dimple', 20, '男')  # 设置指定位置
#        ↑                     ↑       ↑    ↑
#        └──———————————————— → 0       1    2
res3 = '{name} {age} {sex}'.format(name='dimple', sex='男', age=21)  # 设置指定位置
#         |      |    name          ↑                        ↑
#         |______|__________________|           age          |
#                |___________________________________________|
print(res1)
print(res2)
print(res3)
# dimple 18 男
# 20 dimple 男
# dimple 21 男

split():

split() 方法通过指定分隔符对字符串进行切片

name = 'root:0:0::/root:/bin/bash'
print(name.split(':'))  # 默认分隔符为空格
# ['root', '0', '0', '', '/root', '/bin/bash']

name = 'C:a/b/c/d.txt'  # 只想拿到顶级目录
print(name.split('/', 1))
# ['C:a', 'b/c/d.txt']

name = 'C:a/b/c/d.txt'  # 拿到d.txt
print(name.split('/', 4)[3])
# d.txt

name = 'd|im|ple'
print(name.split('|', 2))  # 切割‘|’ 默认从左到右切分
# ['d', 'im', 'ple']

join():

join() 方法用于将序列中的元素以指定的字符连接生成一个新的字符串

tag = '-'
print(tag.join(['dimple', 'say', 'hello', 'world']))  # 可迭代对象必须都是字符串
# dimple say hello world

replace():

replace() 方法把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次。

name = 'dimple say : i have a dream,is a beautiful dream'
print(name.replace('dream', 'girlfriend', 1))
# dimple say : i have a girlfriend,is a beautiful dream

isdigit():

isdigit()可以判断bytes和unicode类型,是最常用的用于于判断字符是否为"数字"的方法

age = input('>>')
print(age.isdigit())  # 返回True和False

find,rfind,index,rindex,count:

name = 'dimple say : i have a girlfriend'
print(name.find('dimple', 0, 6))  # 顾头不顾尾,找不到则返回-1不会报错,找到了则显示索引
# print(name.index('e',2,4)) #同上,但是找不到会报错
print(name.count('d', 1, 3))  # 顾头不顾尾,如果不指定范围则查找所有
# 找到则返回0 找不到则返回-1

center,ljust,rjust,zfill:

center() 方法返回一个指定的宽度 width 居中的字符串,fillchar 为填充的字符,默认为空格。
center 语法:
str.center(width[, fillchar])

name = 'dimple'
print(name.center(30, '-'))  # ------------dimple------------   原字符居中
print(name.ljust(30, '-'))  # dimple------------------------ 原字符左对齐
print(name.rjust(30, '-'))  # ------------------------dimple 原字符右对齐
print(name.zfill(50))  # 用0填充 # 00000000000000000000000000000000000000000000dimple 原字符右对齐

expandtabs():

expandtabs() 方法把字符串中的 tab 符号 转为空格,
tab 符号 默认的空格数是 8,在第 0、8、16...等处给出制表符位置,
如果当前位置到开始位置或上一个制表符位置的字符数不足 8 的倍数则以空格代替。

name = 'dimple	hello	world'
print(name)  # dimple  hello  world
print(name.expandtabs(1))  # dimple hello world  #设置空一个空格
# dimple   hello  world
# dimple hello world

capitalize,swapcase,title:

capitalize()将字符串的第一个字母变成大写,其他字母变小写
swapcase() 方法用于对字符串的大小写字母进行转换。
title()方法返回"标题化"的字符串, 就是说所有单词的首个字母转化为大写,其余字母均为小写

msg = 'dimple like a grill'
print(msg.capitalize())  # 首字母大写 Dimple like a grill
print(msg.swapcase())  # 大小写翻转 DIMPLE LIKE A GRILL
print(msg.title())  # 每个单词的首字母大写 #Dimple Like A Grill
原文地址:https://www.cnblogs.com/x945669/p/13595472.html