Select to view content in your preferred language

Truncate String - Remove Space

5143
17
Jump to solution
09-26-2017 11:41 AM
by Anonymous User
Not applicable

Hello,


I'm trying to implement a truncation in my script, which is easy enough, but I need to make the overall script smart enough to remove the last character if it happens to be a space. 

For instance, if I have the following code:

  1. variable = "SAMPLE NAME"
    truncVar = variable[:7]‍‍‍‍‍‍‍‍

This ends up returning "SAMPLE ", which is right, but there's a space at the end of the string.  I need the script to check for that space, and delete it if it's there, otherwise leave the last character.  I can't just reduce the truncated value since the actual names in the variables will vary widely. 

Thanks!

Working Script based on correct answer:

variable = "This is a test variable"
trunc = variable[:10].strip()
result = "'" + trunc + "'"
print result
'This is a' # The strip operation removed the ending space, but it left all others
0 Kudos
17 Replies
JamesCrandall
MVP Alum

I was under the impression that this also needs to account for "SAMPLE NAME" or "SAM PLE NAME"

by Anonymous User
Not applicable

James Crandall‌, I didn't test this out fully enough.  It worked on the sample I had above, but I ran this same test on different variables and it didn't quite work.  I was a bit too hasty there. 

variable = "SAMPLE NAME"
trunc = variable[:8]
string = "'" + trunc + "'"
print string
'SAMPLE N' # last character is not a space

truncRemove = trunc[:trunc.rfind(' ')]
string2 = "'" + truncRemove + "'"
print string2
'SAMPLE' # I don't want the "_N" removed

# Since the last character returned in trunc is not a space, the desired result is what was returned in the first print string operation

Thanks for catching that, jamesfreddyc

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Mitch Holley‌ already provided the answer, i.e., set your truncation width and then simply run strip() to remove any leading or trailing spaces.

>>> variable = "SAMPLE NAME"
>>> trunc = variable[:7].strip()
>>> string = "'" + trunc + "'"
>>> print string
'SAMPLE'
>>> 
by Anonymous User
Not applicable

This does seem to check out.  I was under the impression that strip would remove all spaces, or only a specified character. The example at the link that he provided showed the removal of only the zeros from the string, and that didn't seem to be what I needed. 

Thanks for clarifying. 

mitchh300‌, thank you for that suggestion.  I have marked your answer as correct. 

0 Kudos
JamesCrandall
MVP Alum

You will still need to account for the possible conditions. 

This will work for "SAMPLE NAME".

This will fail for "SAM PLE NAME"

variable = "SAM PLE NAME"
trunc = variable[:7].strip()
string = "'" + trunc + "'"
0 Kudos
by Anonymous User
Not applicable

Whenever I run the above code that you provided, I get "SAM_PLE" as a result, which is correct given that that makes up the 7 characters.  If I extend that out to 8 characters to grab that extra space, the strip function removes the last space and just returns the 7 characters, which is the desired result since I only want to remove the ending space.

Do you get something different, or are we crossing wires on what the desired results are?

0 Kudos
JamesCrandall
MVP Alum

if variable = "SAM PLE NAME" then the string result will be "SAM PLE", not "SAMPLE".  I was in response to what Joshua posted

>>> variable = "SAMPLE NAME"
>>> trunc = variable[:7].strip()
>>> string = "'" + trunc + "'"
>>> print string

0 Kudos
by Anonymous User
Not applicable

I see what you are saying now.  We were crossing wires a bit.  I probably didn't explain what was needed very well.  In using "SAM PLE" as my variable, I wasn't trying to indicate that I wanted them joined, I was just trying to simulate a space, and I used the original variable name that I used from the start so that probably came across confusingly.  I'll use a better example below:

variable = "This is a test variable"
trunc = variable[:10].strip()
result = "'" + trunc + "'"
print result
'This is a'

The strip operation removed the last space in the string, but it left all other spaces, which is as desired.