Languages
[Edit]
EN

Python - trim whitespace from string

0 points
Created by:
Haris65
536

In this article, we would like to show you how to trim whitspace from a string in Python.

Quick solution:

text = "   abc  "
print(text.strip())  # "abc"

 

Practical example

In this example, we use strip() method to trim whitespace from the beginning and from the end of the string.

text1 = "   abc"
text2 = "abc   "
text3 = "   abc   "

print(text1.strip())  # abc
print(text2.strip())  # abc
print(text3.strip())  # abc

Output:

abc
abc
abc
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Cross technology - trim whitespace from string

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join