Select to view content in your preferred language

Truncating String in Python

13981
6
06-10-2011 11:44 AM
MikeMacRae
Frequent Contributor
I have poured through the internet trying to find a way to truncate a string in Python and I am at a wall.

I want to truncate a string like this:

My_String_Needs_to_be_truncated_12May11_mm

I want to truncate this portion "_12May11_mm" so that I have "My_String_Needs_to_be_truncated" remains.

Thanks for any help!
Mike
Tags (2)
0 Kudos
6 Replies
JasonScheirer
Esri Alum
What do other strings of the same form look like? Do you need to truncate to a length or a number of fields?

x = "My_String_Needs_to_be_truncated_12May11_mm"
truncated = x[:31]


or

x = "My_String_Needs_to_be_truncated_12May11_mm"
truncated = x.rsplit("_")[0]


etc. What do other samples of strings you need truncating look like?
0 Kudos
DarrenWiens2
MVP Alum
If you want to remove a certain number of characters from the right, use rstrip(). Alternatively, you can return a list of "words" contained in a string separated by a given character using split(), then keep the ones you want. Your best bet is to Google "python string operations".
0 Kudos
MikeMacRae
Frequent Contributor
Hey thanks for the replies.

Jason, all my strings look like that. They all end exactly like my example with a "_12May11_mm" The exact amount of characters in that portion never changes (11) because I only change the date and the "mm" are my initials. If another user will be using my profile, the initials may change, but it's always just 2 characters. The amount of characters in the leading portion (My_String_Needs_to_be_truncated) can and will change often, so I wanted to tackle this from the last portion because it is more consistent.

Basically, this is the the format of my mxd file names. When my program updates the mxd, I want to resave the file with the current date and the users initials. I have a line that can do the update, I am just having trouble seperating the file name and then grabbing that first portion into a variable or something. Below is my code for updating the file name. I would like to store the "My_String_Needs_to_be_truncated" into the variable "sitename"

"Path Name:"  + outloc + "\\" + sitename + "_"  + "_" + strftime("%y%b%d") + "_" + user
0 Kudos
TerrySilveus
Frequent Contributor
Here's a couple more options
# this option removes the 11 right characters (if you know it will always be 11)
x = 'My_String_Needs_to_be_truncated_12May11_mm'
print x[0:-11]

# this option builds the string after putting it into an array split at the "_" and removing the last two elements
y = x.split("_")
y.pop()
y.pop()
z = ""
i = 1
for word in y:
    if i == 1:
        z = z + word
    else:
        z = z + "_" + word
    i = 2

print z
0 Kudos
JasonScheirer
Esri Alum
Ah, if it's always the same like that then you can do this:

tail_to_clip = "_12May11_mm"
x = "My_String_Needs_to_be_truncated_12May11_mm"
truncated = x.rsplit("_")[:-len(tail_to_clip)]


And then just swap out tail_to_clip as needed. Though the option with rstrip is a bit more robust.
0 Kudos
MikeMacRae
Frequent Contributor
Thanks everyone, for the help. I used a combination of your suggestions and now that portion of my script works perfectly!

Cheers,
Mike
0 Kudos