python中删掉行_在Python中删除尾随的新行

python中删掉行

Python supports inbuilt methods called strip(), lstrip() and rstrip() which works on string variable and removes the trailing newline or spaces as per given argument.

Python支持称为strip(),lstrip()和rstrip()的内置方法这些方法可处理字符串变量,并根据给定参数删除尾随的换行符或空格。

1)strip() (1) strip())

The strip() method remove characters from both left and right based on the argument. It returns a copy of the string with both leading and trailing characters stripped.

strip()方法根据该参数从左右两个方向删除字符。 它返回字符串的副本,其中删除了前导字符和尾随字符。

When the combination of characters in the chars argument mismatches the character of the string in the left/right, the method stops removing the leading/trailing characters respectively,

当chars参数中的字符组合与左/右字符串的字符不匹配时,该方法将停止分别删除前导/尾随字符,

Syntax:

句法:

    string.strip([chars])

Here, the chars are an optional argument. If not provided, all the leading and trailing whitespaces shall be removed.

在这里, 字符是一个可选参数。 如果未提供,则应删除所有前导和尾随空格。

Example for strip()

strip()的示例

>>> test_string = "   include help is learning source  "
>>> print(test_string.strip())
include help is learning source
>>> print(test_string.strip('   include'))
help is learning sour #here along with leading space and the word 'include', the char 'e' will be removed from the trailing
>>> print(test_string.strip('   test'))
include help is learning sourc
>>> test_str = "simple string"
>>> print(test_str.strip('s'))
imple string

2)lstrip() (2) lstrip())

The lstrip() method returns a copy of the string with leading characters removed based on the argument passed. Similar to strip(), all the combinations of characters in the argument are removed from the left of the string, until the first mismatch.

lstrip()方法返回字符串的副本,该字符串的副本根据传递的参数删除了前导字符。 与strip()相似,参数中的所有字符组合都将从字符串的左侧删除,直到第一个不匹配为止。

Syntax:

句法:

    string.lstrip([chars])

chars are an optional argument which if not provided, removes all leading whitespaces from the string.

chars是一个可选参数,如果未提供,则从字符串中删除所有前导空格。

Example of usage of lstrip()

lstrip()的用法示例

>>> test_string = '   test string'
>>> print(test_string.lstrip())
test string
>>> print(test_string.lstrip('test'))
   test string
>>> print(test_string.lstrip('    test'))#here along with leading space the chars 'st' from 'string' is also removed
ring

3)rstrip() (3) rstrip())

The rstrip() method returns a copy of the string with trailing spaces removed based on the arguments passed. Here trailing is the right based argument. Similar to strip() and lstrip(), all the combinations of characters in the chars argument are removed from the right of the string until the first mismatch.

rstrip()方法返回字符串的副本,该字符串的副本根据传递的参数删除了尾随空格。 尾随是基于权利的论点。 与strip()和lstrip()类似,从字符串的右边删除chars参数中所有字符组合,直到第一个不匹配为止。

Syntax:

句法:

    string.rstrip([chars])

Here, the chars are optional argument, which if not provided all the whitespaces are removed from the string.

在这里, chars是可选参数,如果未提供,则将从字符串中删除所有空格。

Example usage of rstrip()

rstrip()的示例用法

>>> test_string = 'sample test string    '
>>> print(test_string.rstrip())
sample test string
>>> test_string = 'sample test string    test\n'
>>> print(test_string)
sample test string    test

>>> print(test_string.rstrip('\n'))
sample test string    test
>>>


翻译自: https://www.includehelp.com/python/remove-trailing-new-line.aspx

python中删掉行

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值