KnowledgeBoat Logo
|

Computer Science

What will be the output of the following programming code?

str = "My Python Programming"
print (str[-5:-1])
print (str[1:5])
print (str[:-4])
print (str[0:])
print (str[:13-4])
print (str[:3])

Python

Python String Manipulation

3 Likes

Answer

mmin
y Py
My Python Program
My Python Programming
My Python
My 

Working

  1. str[-5:-1] extracts the substring from the 5th last character up to, but not including, the last character: "mmin".
  2. str[1:5] extracts the substring from index 1 to 4: "y Py".
  3. str[:-4] extracts the substring from the beginning up to, but not including, the last 4 characters.
  4. str[0:] extracts the substring from index 0 to the end.
  5. str[:13-4] is equivalent to str[:9], which extracts the substring from the beginning up to, but not including, index 9: "My Python".
  6. str[:3] extracts the substring from the beginning up to, but not including, index 3: "My ".

Answered By

1 Like


Related Questions