Function equivalent to trim (function to remove extra spaces at the beginning and end) in python?

6

In PHP and javascript, we have the trim function that serves to trim string , removing extra spaces at the beginning and at the end.

So:

 $str = ' meu nome é wallace ';
 var_dump(trim($str)); // string('meu nome é wallace')

How can I do this in python ?

I could not find any function similar to trim .

    
asked by anonymous 12.01.2016 / 12:04

5 answers

11

.strip

According to the manual , there is the .strip() method:

minhaString.strip()

If you need to specify characters to be removed, such as spaces only, leaving tabs and line breaks, you can use this:

minhaString.strip(" ")

Note that in this second case, if you specify multiple characters, they are treated individually:

"tomate seco".strip("otm")

Return:

ate sec


.rstrip and .lstrip

If you need to remove only to the left, you have .lstrip() :

"banana".lstrip( "abc" )

Return:

nana

If you need to remove only to the right, you have .rstrip() :

"banana".lstrip( "a" )

Return:

banan

The default of .rstrip and .lstrip is also removing spacing characters such as tabs and line breaks.


Deprecated

Just to note, this existed as a function:

string.strip( string [, caracteres] )

It worked the same way as described above, but string was the 1st argument of the call, and the second, optional, characters to remove. Likewise, if omitted, it refers to whitespace, tabs, and line breaks.

I'm referring in the past, but it will work until it's actually removed.

    
12.01.2016 / 12:12
5

Does not have function with the name of trim() but has strip() that does the equivalent work. It removes the characters \n, \r, \t, \f, espaço

" meu nome é wallace ".strip()

Based on : Trimming the string in Python

    
12.01.2016 / 12:11
4

What you are looking for is .strip () . It is responsible for removing spaces at the beginning and end, such as trim() .

' meu nome é wallace '.strip()

You have other options, such as .replace () .

str= ' meu nome é wallace '
str.replace(" ", "")

Remembering that in this way you will remove all, not only the beginning and end, as the trim .

Also remember that in python you also have .rstrip () and .lstrip () , for removal of right and left characters respectively.

As the question is about PHP > Python , there is the php2python site just for that.

Fonts: Trimming the string in Python
How to trim whitespace (including tabs)? Python Handbook

    
12.01.2016 / 12:26
2

Complementing.

To remove all whitespace you can use join and split as suggested in the comments by Guilherme Lima , see example: p>

s = " Stack Over Flow "
s = ''.join(s.split())

print s

Output: StackOverFlow

Or a regular expression:

import re

s = " Stack Over Flow "    
pattern = re.compile(r'\s+')    
s = re.sub(pattern, '', s)    
print s

Output: StackOverFlow

And to remove only whitespace at the beginning and end of the string use strip as suggested by users rray , Bacco and Randrade < strong>:

s = " Stack Over Flow "

s = s.strip()

print s

Output: Stack Over Flow

See the re documentation for more. See the documentation for common string operations to learn more about the join , split and strip .

Source: Python removes all whitespace in a string

    
12.01.2016 / 16:13
1

To make a trim function in python that serves to trim string, removing extra spaces at the beginning and at the end, just use Regular Expression indicating whitespace at the end (r '\ s + $') and at the beginning of the String (r '^ \ s +'), see:

Entry: "Stack Over Flow"

import re

s = " Stack Over Flow "
print "'"+s+"'"
pattern = re.compile(r'\s+$')    
s = re.sub(pattern, '', s)
pattern = re.compile(r'^\s+')
s = re.sub(pattern, '', s)
print "'"+s+"'"

Output: "Stack Over Flow"

    
05.09.2016 / 01:26