Truncate String - Remove Space

3232
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
1 Solution

Accepted Solutions
MitchHolley1
MVP Regular Contributor

Look into strip().  If you don't pass any parameters, the function will remove spaces before and after a string.  I don't know if that will help or not. 

View solution in original post

17 Replies
JamesCrandall
MVP Frequent Contributor

I'd think split() is a better method as long as the space between the two words is how you identify what to evaluate.  That way it doesn't have to be a set number of characters to the left or right.

variable = "SAMPLE NAME"
splitVar1 = variable.split()
print splitVar1[0]
by Anonymous User
Not applicable

I should have expanded a bit more. There is a potential that there could be a space somewhere before the truncated number of characters. 

So instead of "Sample_Name" with only one space, we could potentially have "Sam_ple_Name" with two spaces.  In the second instance, I need to only remove the last space. 


*NOTE* I understand that [:7] as highlighted in my original post would not return both spaces in my second scenario, but the [:7] is theoretical for example only.  There are inevitably going to be examples where I have two spaces, but only need one removed if it happens to be the last character. 

0 Kudos
JamesCrandall
MVP Frequent Contributor

Could you clarify that you want  "SAMPLE" as an output from these possibilities?

"SAMPLE NAME"

"SAM PLE NAME"

" SAM PLE NAME"

" SAM PLE NAME "

0 Kudos
DanPatterson_Retired
MVP Emeritus

are you trying to split on spaces? How complex is it going to get? Do you want to close the gap except for the last one? like this one?

a = 'Sam ple space'
a.split(' ')[:-1]
['Sam', 'ple']
# ----  now slice and splice in one go

"".join(a.split(' ')[:-1])
'Sample'
by Anonymous User
Not applicable

jamesfreddyc‌, the variable is the name given to individual features (1000's of them), so there will never be a space at the beginning, but there could be a space anywhere in the name of the feature.  The variable is part of a concatenation script, but I need that concatenation script to adhere to a maximum set of characters.  Since the variable names can range from only a few characters on up to ~100 characters, I need to truncate it.  All other parts of the concatenation are standard, so I need to truncate the feature name to 14 characters or less, which places me right at the limit required.  The problem is that there are instances where the 14th character just so happens to land on a space, and I need to remove that last space in those instances. 

Dan_Patterson‌, I don't need to join the strings back together or anything, I just need to remove the last one. 

My end concatenation looks something like this:

(AB-CD-EFGHIJKL-12345)-(Variable)-(XYZ)-(01)

The variable portion is what I'm trying to fix.  If the space is left there, then there's an unnecessary space that we want to remove.  Instead of "(SAMPLE_)", we need "(SAMPLE)", however, if the name so happens to be "SAM PLE" then we want to retain that space and end up with "(SAM_PLE)". 

0 Kudos
MitchHolley1
MVP Regular Contributor

Look into strip().  If you don't pass any parameters, the function will remove spaces before and after a string.  I don't know if that will help or not. 

JamesCrandall
MVP Frequent Contributor

The variable portion is what I'm trying to fix.  If the space is left there, then there's an unnecessary space that we want to remove.  Instead of "(SAMPLE_)", we need "(SAMPLE)", however, if the name so happens to be "SAM PLE" then we want to retain that space and end up with "(SAM_PLE)". 

I'm not entirely skilled in string manipulation and there's likely far better solution -- so this is quite limited in scope and would only account for the above scenario if the variable is "SAMPLE " or "SAM PLE NAME".  But perhaps it might give you some ideas.

truncVar = ""
variable = "SAM PLE NAME"
splitVarList = variable.split()

if len(splitVarList) < 3:
    truncVar = splitVarList[0].strip()
elif len(splitVarList) == 3:
    truncVar = "%s%s" % (splitVarList[0].strip(),splitVarList[1].strip())

print truncVar
0 Kudos
KevinDunlop
Occasional Contributor III

Try

variable = "SAMPLE NAME"
truncVar = variable[:variable.rfind(' ')]‍‍
by Anonymous User
Not applicable

That did the trick.  Thanks, Kevin Dunlop‌!

This is exactly what I was looking for: