python字符串的使用方法_python输入字符串str
2023-06-13 09:12:51 时间
大家好,又见面了,我是你们的朋友全栈君。
python字符串常用方法
find(sub[, start[, end]])
在索引start和end之间查找字符串sub 找到,则返回最左端的索引值,未找到,则返回-1 start和end都可省略,省略start说明从字符串开头找 省略end说明查找到字符串结尾,全部省略则查找全部字符串
source_str = "There is a string accessing example"
print(source_str.find('r'))
>>> 3
count(sub, start, end)
返回字符串sub在start和end之间出现的次数
source_str = "There is a string accessing example"
print(source_str.count('e'))
>>> 5
replace(old, new, count)
old代表需要替换的字符,new代表将要替代的字符,count代表替换的次数(省略则表示全部替换)
source_str = "There is a string accessing example"
print(source_str.replace('i', 'I', 1))
>>> There Is a string accessing example # 把小写的i替换成了大写的I
split(sep, maxsplit)
以sep为分隔符切片,如果maxsplit有指定值,则仅分割maxsplit个字符串 分割后原来的str类型将转换成list类型
source_str = "There is a string accessing example"
print(source_str.split(' ', 3))
>>> ['There', 'is', 'a', 'string accessing example'] # 这里指定maxsplit=3,代表只分割前3个
startswith(prefix, start, end)
判断字符串是否是以prefix开头,start和end代表从哪个下标开始,哪个下标结束
source_str = "There is a string accessing example"
print(source_str.startswith('There', 0, 9))
>>> True
endswith(suffix, start, end)
判断字符串是否以suffix结束,如果是返回True,否则返回False
source_str = "There is a string accessing example"
print(source_str.endswith('example'))
>>> True
lower
将所有大写字符转换成小写
upper
将所有小写字符转换成大写
join
将列表拼接成字符串
list1 = ['ab', 'cd', 'ef']
print(" ".join(list1))
>>> ab cd ef
切片反转
list2 = "hello"
print(list2[::-1])
>>> olleh
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/164969.html原文链接:https://javaforall.cn
相关文章
- 浙江新增python编程_9月起,浙江省八年级新增Python编程课,未来编程是处理大数据的手段…「建议收藏」
- 简述python变量的命名规则_Python 变量命名规则
- 一些常见的Python新手问题
- Python 单链表
- 【说站】python isidentifier()方法是什么
- 【说站】python中pyquery初始化的方法
- python输出unicode编码_Python以utf8编码读取文件
- Python保存json_python保存json文件
- 【7】python_matplotlib 输出(保存)矢量图方法;画图时图例说明(legend)放到图像外侧;Python_matplotlib图例放在外侧保存时显示不完整问题解决
- Python接口自动化之ExtentHTMLTestRunner测试报告
- 软件测试|Python流程控制,你真的会了吗(二)
- Python list列表删除元素(4种方法)
- Linux查看Python版本的有效方法(linux查看python版本)
- python基础教程之简单入门说明(变量和控制语言使用方法)
- python中使用sys模板和logging模块获取行号和函数名的方法
- 跟老齐学Python之编写类之四再论继承
- Python实现拼接多张图片的方法
- Python中让MySQL查询结果返回字典类型的方法