Rectangle 27 0

string How do I trim whitespace with Python?


astringexample
import re
print re.sub('[\s+]', '', s)
s = "  \t a string example\t  "
s = s.strip()
s = s.lstrip()
s = s.rstrip()
s = s.strip(' \t\n\r')

As thedz points out, you can provide an argument to strip arbitrary characters to any of these functions like this:

Results for the examples should be quite helpful :)

The examples above only remove strings from the left-hand and right-hand sides of strings. If you want to also remove characters from the middle of a string, try re.sub:

This will strip any space, \t, \n, or \r characters from the left-hand side, right-hand side, or both sides of the string.

Whitespace on the left side:

Whitespace on the right side:

str is a type, so you should avoid using it as a variable name..

strip() takes in an arguemnt to tell it what to trip. Try: strip(' \t\n\r')

this function does not seem to strip preceding tabs..

Note
Rectangle 27 0

string How do I trim whitespace with Python?


astringexample
import re
print re.sub('[\s+]', '', s)
s = "  \t a string example\t  "
s = s.strip()
s = s.lstrip()
s = s.rstrip()
s = s.strip(' \t\n\r')

As thedz points out, you can provide an argument to strip arbitrary characters to any of these functions like this:

Results for the examples should be quite helpful :)

The examples above only remove strings from the left-hand and right-hand sides of strings. If you want to also remove characters from the middle of a string, try re.sub:

This will strip any space, \t, \n, or \r characters from the left-hand side, right-hand side, or both sides of the string.

Whitespace on the left side:

Whitespace on the right side:

str is a type, so you should avoid using it as a variable name..

strip() takes in an arguemnt to tell it what to trip. Try: strip(' \t\n\r')

this function does not seem to strip preceding tabs..

Note
Rectangle 27 0

string How do I trim whitespace with Python?


#how to trim a multi line string or a file

s=""" line one
\tline two\t
line three """

#line1 starts with a space, #2 starts and ends with a tab, #3 ends with a space.

s1=s.splitlines()
print s1
[' line one', '\tline two\t', 'line three ']

print [i.strip() for i in s1]
['line one', 'line two', 'line three']




#more details:

#we could also have used a forloop from the begining:
for line in s.splitlines():
    line=line.strip()
    process(line)

#we could also be reading a file line by line.. e.g. my_file=open(filename), or with open(filename) as myfile:
for line in my_file:
    line=line.strip()
    process(line)

#moot point: note splitlines() removed the newline characters, we can keep them by passing True:
#although split() will then remove them anyway..
s2=s.splitlines(True)
print s2
[' line one\n', '\tline two\t\n', 'line three ']
Note
Rectangle 27 0

string How do I trim whitespace with Python?


#how to trim a multi line string or a file

s=""" line one
\tline two\t
line three """

#line1 starts with a space, #2 starts and ends with a tab, #3 ends with a space.

s1=s.splitlines()
print s1
[' line one', '\tline two\t', 'line three ']

print [i.strip() for i in s1]
['line one', 'line two', 'line three']




#more details:

#we could also have used a forloop from the begining:
for line in s.splitlines():
    line=line.strip()
    process(line)

#we could also be reading a file line by line.. e.g. my_file=open(filename), or with open(filename) as myfile:
for line in my_file:
    line=line.strip()
    process(line)

#moot point: note splitlines() removed the newline characters, we can keep them by passing True:
#although split() will then remove them anyway..
s2=s.splitlines(True)
print s2
[' line one\n', '\tline two\t\n', 'line three ']
Note
Rectangle 27 0

string How do I trim whitespace with Python?


#how to trim a multi line string or a file

s=""" line one
\tline two\t
line three """

#line1 starts with a space, #2 starts and ends with a tab, #3 ends with a space.

s1=s.splitlines()
print s1
[' line one', '\tline two\t', 'line three ']

print [i.strip() for i in s1]
['line one', 'line two', 'line three']




#more details:

#we could also have used a forloop from the begining:
for line in s.splitlines():
    line=line.strip()
    process(line)

#we could also be reading a file line by line.. e.g. my_file=open(filename), or with open(filename) as myfile:
for line in my_file:
    line=line.strip()
    process(line)

#moot point: note splitlines() removed the newline characters, we can keep them by passing True:
#although split() will then remove them anyway..
s2=s.splitlines(True)
print s2
[' line one\n', '\tline two\t\n', 'line three ']
Note
Rectangle 27 0

string How do I trim whitespace with Python?


str.strip() #trim
str.lstrip() #ltrim
str.rstrip() #rtrim
strip
trim
Note
Rectangle 27 0

string How do I trim whitespace with Python?


something = "\t  please_     \t remove_  all_    \n\n\n\nwhitespaces\n\t  "

    something = "".join(something.split())

Simple and efficient. Could use " ".join(... to keep words separated with a space.

Note
Rectangle 27 0

string How do I trim whitespace with Python?


str.strip() #trim
str.lstrip() #ltrim
str.rstrip() #rtrim
strip
trim
Note
Rectangle 27 0

string How do I trim whitespace with Python?


something = "\t  please_     \t remove_  all_    \n\n\n\nwhitespaces\n\t  "

    something = "".join(something.split())

Simple and efficient. Could use " ".join(... to keep words separated with a space.

Note
Rectangle 27 0

string How do I trim whitespace with Python?


something = "\t  please_     \t remove_  all_    \n\n\n\nwhitespaces\n\t  "

    something = "".join(something.split())

Simple and efficient. Could use " ".join(... to keep words separated with a space.

Note