33

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

Mike
  • 4,099
  • 17
  • 61
  • 83
  • 3
    It's called a slice and the operation is called slicing. – kindall Apr 05 '12 at 19:00
  • 4
    See [this famous question](http://stackoverflow.com/questions/509211/good-primer-for-python-slice-notation). – DSM Apr 05 '12 at 19:02
  • This diagram, in the link provided by @DSM: http://stackoverflow.com/a/509297/401828 – heltonbiker Apr 05 '12 at 19:07
  • 1
    Thank you for naming this kindall and thanks for the stack post reference DSM! – Mike Apr 05 '12 at 19:09
  • The question is not really about string truncation. I recommend that the question title be edited to say something like, "What is the name of the python feature for which indices contain a colon such as `string[0:3]`?" – Toothpick Anemone Aug 07 '22 at 23:31

3 Answers3

39

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

msouth
  • 832
  • 11
  • 21
Uku Loskit
  • 40,868
  • 9
  • 92
  • 93
  • 3
    Thanks a lot Uku! Thank you for giving it a name and a good example on how it works. Much appreciated! – Mike Apr 05 '12 at 19:08
13

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.

Guedes
  • 189
  • 8
2

It's called slicing, read more about it e.g. here: http://docs.python.org/tutorial/introduction.html#strings

thebjorn
  • 26,297
  • 11
  • 96
  • 138