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:
variable <SPAN class="operator token">=</SPAN> <SPAN class="string token">"SAMPLE NAME"</SPAN>
truncVar <SPAN class="operator token">=</SPAN> variable<SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="number token">7</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN></SPAN>
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 <SPAN class="operator token">=</SPAN> <SPAN class="string token">"This is a test variable"</SPAN>
trunc <SPAN class="operator token">=</SPAN> variable<SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="number token">10</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN>strip<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
result <SPAN class="operator token">=</SPAN> <SPAN class="string token">"'"</SPAN> <SPAN class="operator token">+</SPAN> trunc <SPAN class="operator token">+</SPAN> <SPAN class="string token">"'"</SPAN>
<SPAN class="keyword token">print</SPAN> result
<SPAN class="string token">'This is a'</SPAN> <SPAN class="comment token"># The strip operation removed the ending space, but it left all others</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>