Slicing: Accessing more than one character from the string.
Syntax :
[lower : upper : step]
- Specifying the above values are optional
- Lower value by default 0
- Upper value by default len(string)
- Step value is 1 by default.
s = "python"
print("String is :",s)
print("s[:] :", s[:])
print("s[: :] :", s[: :])
print("s[0:6:1] :", s[0:6:1])
print("s[0:len(s)] :", s[0:len(s)])
Analyze this code snippet:
s = "python"
print("String is :",s)
print("s[2:5] :", s[2:5])
print("s[-5:-2] :", s[-5:-2])
print("s[2:-2] :", s[2:-2])
Output this code snippet:
s = "python"
print("String is :",s)
print("s[5:2:-1] :", s[5:2:-1])
print("s[-1:-5:-1] :", s[-1:-5:-1])