How to trim leading and trailing Whitespace from a Text String in Python

Cloudmersive
1 min readJun 9, 2020

--

Whitespace trimming has all kinds of practical applications, but you probably don’t need me to explain that. Instead, I shall be delving into an easy means by which you can implement this type of feature into your project with as little effort required on your part as possible. Sound good? Read on.

Pip install the client to begin:

pip install cloudmersive-convert-api-client

And from there, our function call.

from __future__ import print_functionimport timeimport cloudmersive_convert_api_clientfrom cloudmersive_convert_api_client.rest import ApiExceptionfrom pprint import pprint# Configure API key authorization: Apikeyconfiguration = cloudmersive_convert_api_client.Configuration()configuration.api_key['Apikey'] = 'YOUR_API_KEY'# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed# configuration.api_key_prefix['Apikey'] = 'Bearer'# create an instance of the API classapi_instance = cloudmersive_convert_api_client.EditTextApi(cloudmersive_convert_api_client.ApiClient(configuration))request = cloudmersive_convert_api_client.RemoveWhitespaceFromTextRequest() # RemoveWhitespaceFromTextRequest | Input requesttry:# Trim leading and trailing whitespace from text stringapi_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)

Finally, toss in your string as part of the request object, and your new loyal API servant will handle the rest.

Wouldn’t you rather be hugging a puppy instead of coding?

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet