如何在Python中删除前导空格?

问题:如何在Python中删除前导空格?

我有一个以多个空格开头的文本字符串,介于2和4之间。

删除前导空格的最简单方法是什么?(即删除某个字符之前的所有内容?)

"  Example"   -> "Example"
"  Example  " -> "Example  "
"    Example" -> "Example"

I have a text string that starts with a number of spaces, varying between 2 & 4.

What is the simplest way to remove the leading whitespace? (ie. remove everything before a certain character?)

"  Example"   -> "Example"
"  Example  " -> "Example  "
"    Example" -> "Example"

回答 0

lstrip()方法将删除以字符串开头的前导空格,换行符和制表符:

>>> '     hello world!'.lstrip()
'hello world!'

编辑

正如balpha在注释中指出的那样,为了仅从字符串开头删除空格,lstrip(' ')应使用:

>>> '   hello world with 2 spaces and a tab!'.lstrip(' ')
'\thello world with 2 spaces and a tab!'

相关问题:

The lstrip() method will remove leading whitespaces, newline and tab characters on a string beginning:

>>> '     hello world!'.lstrip()
'hello world!'

Edit

As balpha pointed out in the comments, in order to remove only spaces from the beginning of the string, lstrip(' ') should be used:

>>> '   hello world with 2 spaces and a tab!'.lstrip(' ')
'\thello world with 2 spaces and a tab!'

Related question:


回答 1

该函数strip将从字符串的开头和结尾删除空格。

my_str = "   text "
my_str = my_str.strip()

将设置my_str"text"

The function strip will remove whitespace from the beginning and end of a string.

my_str = "   text "
my_str = my_str.strip()

will set my_str to "text".


回答 2

如果要剪切单词前后的空格,请保留中间的空格。
您可以使用:

word = '  Hello World  '
stripped = word.strip()
print(stripped)

If you want to cut the whitespaces before and behind the word, but keep the middle ones.
You could use:

word = '  Hello World  '
stripped = word.strip()
print(stripped)

回答 3

要删除某个字符之前的所有内容,请使用正则表达式:

re.sub(r'^[^a]*', '')

删除所有内容,直到第一个“ a”。[^a]可以替换为您喜欢的任何字符类,例如单词字符。

To remove everything before a certain character, use a regular expression:

re.sub(r'^[^a]*', '')

to remove everything up to the first ‘a’. [^a] can be replaced with any character class you like, such as word characters.


回答 4

这个问题不会解决多行字符串,但是这是如何使用python的标准库textwrap模块从多行字符串中去除前导空格。如果我们有一个像这样的字符串:

s = """
    line 1 has 4 leading spaces
    line 2 has 4 leading spaces
    line 3 has 4 leading spaces
"""

如果我们print(s)得到如下输出:

>>> print(s)
    this has 4 leading spaces 1
    this has 4 leading spaces 2
    this has 4 leading spaces 3

如果我们使用了textwrap.dedent

>>> import textwrap
>>> print(textwrap.dedent(s))
this has 4 leading spaces 1
this has 4 leading spaces 2
this has 4 leading spaces 3

The question doesn’t address multiline strings, but here is how you would strip leading whitespace from a multiline string using python’s standard library textwrap module. If we had a string like:

s = """
    line 1 has 4 leading spaces
    line 2 has 4 leading spaces
    line 3 has 4 leading spaces
"""

if we print(s) we would get output like:

>>> print(s)
    this has 4 leading spaces 1
    this has 4 leading spaces 2
    this has 4 leading spaces 3

and if we used textwrap.dedent:

>>> import textwrap
>>> print(textwrap.dedent(s))
this has 4 leading spaces 1
this has 4 leading spaces 2
this has 4 leading spaces 3