Class 11 String Slicing in Python Exercise | Assignments | Best Solved Examples

Share with others

String Slicing in Python Exercise

String Slicing in Python is an important topic for Exams

What is String Slicing in Python?

String slicing in Python is to obtain a substring from the main string. Slice of string means part (substring) of string.

Syntax of String Slicing in Python

<String Name>[Start : Stop : Step]

Start : A position from where we have to start the cutting of string.

Stop : A position before what we have to cut the string.

Step : Number of characters to be skipped during string slicing in python.

NOTE : Position of stop is not included in the slicing
By default Start is 0
By default Step is 1

Solved Examples of String Slicing in Python

Q1. Write the Answers of the following in reference to given String:

str = “string slicing”

Strstringslicing
+ve Index012345678910111213
-ve Index-14-13-12-11-10-9-8-7-6-5-4-3-2-1
String Slicing in Python

a) print(str[3 : 6])

Here Slicing is starting from index 3 (from alphabet ‘i’) and ending position is before 6 (till alphabet ‘g’)

Output : ing

b) print(str[4 : 10])

Here Slicing is starting from index 4 (from alphabet ‘n’) and ending position is before 10 (till alphabet ‘i’)

Output : ng sli

c) print(str[ : ])

Output : string slicing

d) print (str[7 : ])

Output : slicing

e) print(str[-7 : ]

Output : slicing

f) print(str[-9 : -3]

Output : g slic

g) print(str[2 : 10 : 2])

Output : rn l

h) print(str[-1 : -1 : -1])

Output : Nothing will print

h) print(str[-11 : -4])

Output : ing sli

i) print(str[-14 : : -3])

Output : si in

j) print( str[ : : -3])

Output : gcsnt

Exercise of String Slicing – 1

Q1. Write the output of the following code

str = “Welcome to my blog”

a. print( str [3 : 18]

b. print ( str[ 2 : 14 : 2])

c. print( str [ : 7])

d. print( str[8 : -1 : 1])

e. print(str [-9 : -15])

f. print(str[0 : 9 : 3])

g. print(str[9 : 29 : 2])

h. print(str[-6 : -9 : -3])

i. print(str[-9 : -9 : -1])

j. print(str[8 : 25 : 3])

SOLUTIONS :

a) come to my blog

b) loet y

c) Welcome

d) to my blo

e) No Output

f) Wce

g) om lg

h) y

i) No Output

j) tmbg

Exercise of String Slicing – 2

Q1. Write the output of the following :

str = “String Slicing in Python”

a) print(str [13 : 18])

b) print(str[ -2 : -4 : -2])

c) print(str [12 : 18 : 2])

d) print(str[-17 : -1 : 1])

e) print(str[-6 : -20 : -2])

f) print(str[0 : 9 : 3])

g) print(str[19 : 29])

h) print(str[-6 : -9 : -3])

i) print(str[-9 : -0 : -1])

j) print(str[2 : 16 : 3])

SOLUTIONS :

a) g in

b) o

c) n n

d) Slicing in Pytho

e) Pn ncl

f) Si

g) ython

h) P

i) i gnicilS gnirt

j) rgli

Programs Based on String Slicing

Q1. Accept a string from the user and display the string with first character of each word Capital. (Without using inbuilt function)

Ans.

str = input("Enter any String")

str1 = ""

L=str.split()

for i in L:

       str1 = str1 + i[0].upper() + i[1:] +" "

print(str1)

Q2. Write a program to accept a string from the user and display n characters from the left of the string. (Accept n from the user)

Ans. 

str = input("Enter any string")

n=int(input("Enter number of characters to be extract from left"))

L = len(str)

if n > L :

      print("Enter less number of characters")

else:

  print(str[ : n ])

Q3. Write a program to reverse the string using slicing.

Ans.

str = "String Slicing in Python"
print(str[: : -1])

OUTPUT is:

nohtyP ni gnicilS gnirtS

Q4. Write a program to accept a string from the user and display n characters from the right of the string. (Accept n from the user)

Ans. 

str = "String Slicing in Python"
n = int(input("Enter number of characters"))
l = len(str)
print(str[l-n : : ])

Q5. Accept a String from the user and display first two characters of each word in same line separated by space.

Ans.

str = "String Slicing in Python"
word = str.split()
for i in word:
     print(i[0 : 2], end = " ")

OUTPUT :
St Sl in Py 

Important Links :

Class 12 Computer Science Sample Paper 2020-2021.

Class 12 Computer Science Sample Paper Marking Scheme

Class 12 Computer Science Test Series


Share with others

2 thoughts on “Class 11 String Slicing in Python Exercise | Assignments | Best Solved Examples”

Leave a Reply

error: Content is protected !!