Truncating a String in Python

Someone gave me a syntax to truncate a string as follows:

string = "My Text String"

print string [0:3] # This is just an example

I'm not sure what this is called (the string[0:3] syntax), so I've had a hard time trying to look it up on the internet and understand how it works. So far I think it works like this:

  • string[0:3] # returns the first 3 characters in the string
  • string[0:-3] # will return the last 3 characters of the string
  • string[3:-3] # seems to truncate the first 3 characters and the last 3 characters
  • string[1:0] # I returns 2 single quotes....not sure what this is doing
  • string[-1:1] # same as the last one

Anyways, there's probably a few other examples that I can add, but my point is that I'm new to this functionality and I'm wondering what it's called and where I can find more information on this. I'm sure I'm just missing a good reference somewhere.

Thanks for any suggestions, Mike

4

3 Answers

It's called a slice. From the python documentation under Common Sequence Operations:

s[i:j]

The slice of s from i to j is defined as the sequence of items with index k such that i <= k < j. If i or j is greater than len(s), use len(s). If i is omitted or None, use 0. If j is omitted or None, use len(s). If i is greater than or equal to j, the slice is empty.

source

1

As @Uku and @thebjorn said its called Slicing

But one easier way to think is to consider a String like a list, for example you can do:

text = 'Any String'
for letter in text:
    print letter

And the same if you want to get a specific letter inside the string:

>> text = 'Any String'
>> text[4]
'S'

ps.: Remember that it's zero based, so text[4] return the 5th letter.

Using Slice it'll return a "substring" text[i:j] from your original String where "i" are the initial index (inclusive) and "j" are the end index (exclusive), for example:

>> text = 'Any String'
>> text[4:6] # from index 4 to 6 exclusive, so it returns letters from index 4 and 5
'St'
>> text[0:4]
'Any '
>> text[:4] # omiting the "i" index means i = 0
'Any '
>> text[4:] # omitting the "j" index means until the end of the string

A negative index is relative to the end of the String like making a substitution from the negative index to "len(text) + i".

In our case len(text) is 10, a negative index -1 will be like using text[9] to get the last element, -2 will return the last but one element and so forth.

In examples you sent, string[0:-3] should return everything but last 3 characters and string[3:-3] should return everything but first 3 and last 3.

Hope it helpped.

It's called slicing, read more about it e.g. here:

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.

Share this article
Twitter Facebook Pinterest