lstrip和rstrip

Python provides three methods that can be used to trim whitespaces from the string object.

Python提供了三种方法,可用于从字符串对象中修剪空格。

Python修剪字符串 (Python Trim String)

  • strip(): returns a new string after removing any leading and trailing whitespaces including tabs (\t).strip() :删除任何包含制表符(\ t)的前导和尾随空格后,返回一个新字符串。
  • rstrip(): returns a new string with trailing whitespace removed. It’s easier to remember as removing white spaces from “right” side of the string.rstrip() :返回删除了结尾空格的新字符串。 在字符串的“右侧”删除空格时,更容易记住。
  • lstrip(): returns a new string with leading whitespace removed, or removing whitespaces from the “left” side of the string.lstrip() :返回一个新字符串,其中删除了前导空格,或者从字符串的“左侧”删除了空格。

All of these methods don’t accept any arguments to remove whitespaces. If a character argument is provided, then they will remove that characters from the string from leading and trailing places.

所有这些方法都不接受任何用于删除空格的参数。 如果提供了字符参数,则它们将从开头和结尾处的字符串中删除该字符。

Let’s look at a simple example of trimming whitespaces from the string in Python.

让我们看一个简单的示例,该示例从Python中的字符串中修剪空格。

s1 = '  abc  'print(f'String =\'{s1}\'')print(f'After Removing Leading Whitespaces String =\'{s1.lstrip()}\'')print(f'After Removing Trailing Whitespaces String =\'{s1.rstrip()}\'')print(f'After Trimming Whitespaces String =\'{s1.strip()}\'')

Output:

输出:

String ='  abc  '
After Removing Leading Whitespaces String ='abc  '
After Removing Trailing Whitespaces String ='  abc'
After Trimming Whitespaces String ='abc'

Let’s look at some more examples with strings having a new-line and tabs.

我们来看一些带有换行符和制表符的字符串的更多示例。

>>> s1 = '  X\n  Y  \nZ  \t'
>>> s1.strip()
'X\n  Y  \nZ'
>>> s1.rstrip()
'  X\n  Y  \nZ'
>>> s1.lstrip()
'X\n  Y  \nZ  \t'
GitHub Repository.GitHub存储库中签出更多Python字符串示例。

翻译自: https://www.journaldev.com/23625/python-trim-string-rstrip-lstrip-strip

lstrip和rstrip

lstrip和rstrip_Python装饰字符串– rstrip(),lstrip(),strip()相关推荐

  1. Python:字符串基操_strip()/rstrip()/lstrip()_lower()/upper()_startswith()/endswith()_split()/rspilt()_join

    字符串的一些基本用法---->温故而知新,可以为师矣 Python:字符串基操_strip()/rstrip()/lstrip()_lower()/upper()_startswith()/en ...

  2. Python 字符串 rstrip() 方法

    Python有一组可以用于字符串的内置方法.Python 字符串操作常用操作,如字符串的替换.删除.截取.赋值.连接.比较.查找.分割等.本文主要介绍Python 字符串 rstrip() 方法 原文 ...

  3. Python 3 字符串 rstrip( ) 方法(转载)

    Python 3 字符串 rstrip( ) 方法 Python 3.8.5 语法格式: str.rstrip([chars]) 描述: 移除字符串尾部的空格或指定字符串. 移除规则:尾部的字符的移除 ...

  4. Python 中的 lstrip、rstrip、strip

    str.lstrip([chars]) chars -- 指定截取的字符(默认空格). 截掉字符串左边的空格或指定字符. addr = '****123@163.com****' print(addr ...

  5. rstrip python_Python strip()、split()和rstrip()方法

    1. Python strip() 语法描述: Python strip() 方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列. 注意:该方法只能删除开头或是结尾的字符,不能删除中间部 ...

  6. python中字符串str的strip()方法

    strip()还有一种用法是str.strip(c),其中c也是一个字符或者一个字符串.它删除的是str字符串从头开始或者从尾开始带有c含有的字符的所有字符(确实拗口) 比如 str='hiahia ...

  7. python find不区分大小写_牛鹭学院:Python基础了解

    本文来自牛鹭学院学员:田雨 python初印象 Python 是一种解释型语言: 这意味着开发过程中没有了编译这个环节.类似于PHP和Perl语言. Python 是交互式语言: 在一个 Python ...

  8. 【python 1】python 基础

    文章目录 一.格式化输出 1.占位符 2.+ 拼接输出: 3.message 输出: 4.format 输出 二.input 输入 三.pass.break 四.字符串 五.列表 六.元组 七.字典 ...

  9. Python笔记-BeautifulSoup中find_all的使用及str中trim()

    实例是这样的: 将titile和Cost提取,出来 代码如下: from urllib.request import urlopen from bs4 import BeautifulSoupif _ ...

最新文章

  1. Android中怎样使用createTempFile实现将字节数据创建到临时文件并转换成FileOutputStream和FileInputStream
  2. android通讯录上传服务器,Android 实现读取通讯录并上传服务器
  3. Java工程师知识图谱
  4. 【CodeForces - 298B 】Sail (模拟,题意)
  5. mysql部署练习_MySQL主从练习 - osc_b9r67jnt的个人空间 - OSCHINA - 中文开源技术交流社区...
  6. 【303】C# 复制窗体 修改名称
  7. Spring 4.3 的新功能和增强
  8. 『商务口语』2010.01.11--你要在这里停留多久?
  9. DevOps使用教程 华为云(16)git 日志记录 比较2次提交差异
  10. OpenLayers学习入门篇
  11. jquery.form.js的ajaxSubmit和ajaxForm使用
  12. 善领dsa2020最新车机ce版_内功实力再精进 试驾上汽大通V80 PLUS城市版
  13. Win10smb2.0共享至android速度慢问题解决
  14. 友盟分享——微信、腾讯微博、新浪微博分享失败原因分析
  15. Android OTA升级后更新APN参数的实现
  16. 牛客练习赛46-华华跟奕奕玩游戏(期望+逆元)
  17. 如何生成带附加码的EAN13商品条码
  18. 宝塔Linux面板问题QA汇总
  19. oracle bpm 二次开发,Oracle BPM Suite产品详细说明
  20. Modem2G/3G/4G/5G:解决TDS-CDMA:HSDPA:能注册上call box,但无法连接(Integrity protection not activated)

热门文章

  1. [转载] AttributeError: ‘numpy.ndarray‘ object has no attribute ‘insert‘的解决方法
  2. [转载] 生成对角矩阵 numpy.diag
  3. [转载] Python全栈(1)—— Python如何快速下载库与jupyter notebook 的基本使用
  4. 项目Beta冲刺Day4
  5. day006bootstrap的简单学习 + 轮播图
  6. java 通过反射获取数组
  7. 微软Power BI 每月功能更新系列——3月Power BI 新功能学习
  8. 用PHP生成随机数的函数
  9. There is no Action mapped for namespace / and action name .
  10. x264编码参数大测试:05 subme与crf(g)