Day10 - String Slicing

Day10 - String Slicing

Length of String, Positive & Negative Slicing

Length of a String:

We can find length of a string using len() function.

Example 01:

names = "Python, Java"
print(len(names)) #output = 12

Example 02:

fruit = "Mango"
len1 = len(fruit)
print("Mango is a", len1, "letter word.")
#output: Mango is a 5 letter word.

String as an array:

A string is essentially a sequence of characters also called an array. Thus we can access the elements of this array.

Example 01:

pie = "ApplePie"
print(pie[:5]) #output= Apple
print(pie[6])  #output = i (means returns character at specified index)

Example 02:

names = "Python, Javascript"
print(names[0:5]) #output = Python

Slicing of String:

It is used to return a particular range value. Slicing can be: Positive or Negative.

Syntax:

variable = [start : end : step]

  • start = From where, slicing starts. This number will be included.

  • end = From where, slicing starts. This number will be excluded, means "end-1"

  • step: It is the gap between the words/numbers.

    \=> If step = 1 (positive value - means left to right) -> it is by-default

    \=> step = -1 (negative value - means right to left)

Note:

  • For slicing we use square bracket.

  • Step is optional, we can write start and end only e.g. [start : end]

  • Slicing is same for string, list, tuple or dictionary etc.

Example 01:

Length of String:

fruit = "Mango"  #string
mangoLen = len(fruit) 
print(mangoLen)   #output= 5

How it works:

For Length: M a n g o
#           1 2 3 4 5

For Slicing: Positive Indexing: M  a  n  g  o
#                               0  1  2  3  4

For Slicing: Negative Indexing: M    a   n   g   o
#                               -5  -4  -3  -2  -1

Example 02: Positive Slicing:

fruit = "Mango"  

#including 0 but not 4 
print(fruit[0:4])   #Output = Mang

#at start python automatically assumes 0 =>including 0 but not 4 =[0:4]
print(fruit[:4])  #output = Mang

#including 1 but not 4 
print(fruit[1:4])  #output = Man

#at start python automatically assumes 0 
#at end it automatically assumes len of string.
print(fruit[:])   #Output = Mango

Example 03 - Negative String:

fruit = "Mango"
mangoLen = len(fruit) #output = 5

# Negative slicing 
print(fruit[0:-3])  #output = Ma

Note: Negative slicing works as: if we have print(fruit[0:-3]) means total len = -3. Here total len is 5, so 5-3 = 2. So the answer will be print(fruit[0:2]) i.e. 'Ma' #including 0 but not 2

Example 04 - Negative String:

fruit = "Mango"
print(fruit[-1:-3]) #output = error (because 5-1=4 and 5-3=2 
                         #so it will be [4:2] that doesnot possible)

Note: Here python didn't interpret anything, because total len = 5 so for print(fruit[-1:-3]), 5-1 = 4 and 5-3 = 2 then it become 'print(fruit[4:2])' - that doesn't make any sense

Example 05: When Step is also included

String = 'ASTRING'
#                   A  S  T  R  I  N  G
#positive index     0  1  2  3  4  5  6
#negative index    -7 -6 -5 -4 -3 -2 -1

s1 = slice(3) #include 0 end before 3
s2 = slice(1, 5, 2)  #include 1 end before 5 (gap of 2 words)

print("String slicing")
print(String[s1])  #output = AST
print(String[s2])  #output = SR

Output:

String slicing
AST
SR

Example 06:

String = 'GEEKSFORGEEKS'

# Example 1: Slicing from index 0 to 2 (stops at 3-1=2)
print(String[:3])  # Output: GEE

# Example 2: Slicing from index 1 to 5 (stops at 3-1=2), with a step of 2
print(String[1:5:2])  # Output: EK

# Example 3: Slicing from index -1 to -12 (stops at 3-1=2), 
# with a step of -2
print(String[-1:-12:-2])  # Output: SKG

<< Previously ---------------------------------------------------- Next Lesson >>