Select to view content in your preferred language

remove the last word in a string

11028
3
Jump to solution
08-16-2012 10:22 AM
DanielErklauer
Deactivated User
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
Honored 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
Honored 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
Frequent Contributor
" ".join(var.split()[:-1])
Will return the original string, minus the last word.
ColeSutton11
Occasional Contributor

Thank you Bruce. Exactly what I needed.

0 Kudos