How to Trim Whitespace from Text in Python
Because software programs treat whitespace in different ways, it’s important to have a means for quickly trimming whitespace from plain text data when the need arises. Thankfully, including the below API solution in your workflow will make that process easy. It’ll automatically trim trailing and leading whitespace from any text string, speedily returning a whitespace-free text content result. This solution is free & easy to use — just follow steps below to copy ready-to-run Python code examples, and you’ll be finished in minutes.
Let’s start by installing the Python SDK. Run the following command:
pip install cloudmersive-convert-api-client
Now there’s only one step remaining. Include the imports, and call the function:
from __future__ import print_function
import time
import cloudmersive_convert_api_client
from cloudmersive_convert_api_client.rest import ApiException
from pprint import pprint
# Configure API key authorization: Apikey
configuration = cloudmersive_convert_api_client.Configuration()
configuration.api_key['Apikey'] = 'YOUR_API_KEY'
# create an instance of the API class
api_instance = cloudmersive_convert_api_client.EditTextApi(cloudmersive_convert_api_client.ApiClient(configuration))
request = cloudmersive_convert_api_client.RemoveWhitespaceFromTextRequest() # RemoveWhitespaceFromTextRequest | Input request
try:
# Trim leading and trailing whitespace from text string
api_response = api_instance.edit_text_trim_whitespace(request)
pprint(api_response)
except ApiException as e:
print("Exception when calling EditTextApi->edit_text_trim_whitespace: %s\n" % e)
Easy, right? To complete your API call, just include a free-tier Cloudmersive API key (get one by registering a free account here) and you’re all done.