remove the last word in a string

10486
3
Jump to solution
08-16-2012 10:22 AM
DanielErklauer
New Contributor III
How does one remove the last word in a string if the field I am querying has variable character lengths?  I.e:

1234 S Man 77025
123 Main 00865

I want to remove the zip code here but the character counts vary.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MathewCoyle
Frequent Contributor
This should do what you want. Where var is your string.
var.split(" ")[:-1]


If the portion of the string you are trying to remove is always the same length, you could also use the simpler.
var[:-6]

View solution in original post

0 Kudos
3 Replies
MathewCoyle
Frequent Contributor
This should do what you want. Where var is your string.
var.split(" ")[:-1]


If the portion of the string you are trying to remove is always the same length, you could also use the simpler.
var[:-6]
0 Kudos
BruceNielsen
Occasional Contributor III
" ".join(var.split()[:-1])
Will return the original string, minus the last word.
ColeSutton11
New Contributor II

Thank you Bruce. Exactly what I needed.

0 Kudos