-1

I am learning python and I confused on why getting a string subset using the code below works.

s = 'a' print(s[1:])

Since this is a string of length one s[0:] would work since python is 0 indexed. Can somebody help explain why the s[1:] works?

My understanding of substrings is as follows:

string[start:end] = Get all characters from index start to end-1

string[:end] = Get all characters from the beginning of the string to end-1

string[start:] = Get all characters from index start to the end of the string

string[start:end:step] = Get all characters from start to end-1 discounting every step character

Eric
  • 11
  • 2

1 Answers1

-1

You've already explained it

  • string[:end] = Get all characters from the beginning of the string to end-1 In this case,
  • s = 'a' - the length of s = 1
  • print(len(s)) - this will result 1
  • and you are doing s[1:] - print the characters from the start to the end-1,if you try s[0:] - will display - a
Greenonline
  • 1,330
  • 8
  • 23
  • 31
Arun
  • 1
  • 1