1. 概述
在对数据进行预处理的时候,我们经常会用到 strip() 函数来去除字符串或者字段前后的空格,但其实还有一个函数 rstrip(),是用来删除字符串末尾的指定字符(默认为空格),那这两个函数有什么区别呢,rstrip()又怎么使用呢?
2. rstrip()
2.1 语法
str.rstrip([chars])
Python rstrip() 删除 string 字符串末尾的指定字符(默认为空格).
2.2 参数:
2.3 实例
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # @Time : 2019/3/2 20:32 # @Author : Arrow and Bullet # @FileName: rstrip().py # @Software: PyCharm # @Blog :https://blog.csdn.net/_ str1 = " this is string example....wow!!! " str2 = str1.rstrip() # 默认是空格 print(str2) # this is string example....wow!!! str3 = "this is string example....wow!!!" str4 = str3.rstrip("8") print(str4) # this is string example....wow!!! str5 = str3.strip("8") print(str5) # this is string example....wow!!!
3. rstrip() 和 strip()的区别
Python strip() 方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列。
Python rstrip() 删除 string 字符串末尾的指定字符(默认为空格).
希望能够帮助到大家,有什么问题可以 直接评论即可,喜欢有用的话可以点个赞让更多的人看到,如果不够详细的话也可以说,我会及时回复的。
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/221338.html原文链接:https://javaforall.net
