AnsweredAssumed Answered

Truncate String - Remove Space

Question asked by cpotts_extenetsystems on Sep 26, 2017
Latest reply on Sep 26, 2017 by cpotts_extenetsystems


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. 




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