Languages
[Edit]
EN

Python - trim whitespace from string

0 points
Created by:
Dirask Community
6810

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

Python - string (popular problems)

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