Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Remove Whitespace from Both Ends of a String

Python Remove Whitespace from Both Ends of a String

Document Object Reference Manual Python3 Examples

In Python, you can use the strip() method to remove whitespace characters (including spaces, tabs and newlines) from both ends of a string. This method returns a new string; the original string is not modified.

Example

text

=

"   Hello, World!   "

trimmed_text

=

text.

strip

(

)

print

(

f

"Original: '{text}'"

)

print

(

f

"Trimmed: '{trimmed_text}'"

)

Code analysis:

Output result:

Original: '   Hello, World!   '
Trimmed: 'Hello, World!'

Document Object Reference Manual Python3 Examples