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

Share with others

## 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```

## a) print(str[3 : 6])

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

## b) print(str[4 : 10])

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

# Exercise of String Slicing – 2

## 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 wordin 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
```

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”

1. The solution of part (c) of Q1 of Exercise of string slicing -1 is wrong