0% found this document useful (0 votes)
7 views

String Functions Class11 3

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

String Functions Class11 3

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

String manipulation

• String slice: a part of string


• Inorder to access a part of a string, we can give
string_name[starting_index : ending_index]
Eg: word = “Amazing” print(word[1:4]) --> "maz"

0 1 2 3 4 5 6

-7 A -6 m -5 a -4 z-3 i
-2 -1n g

• word[0:7] – ‘Amazing’
• word[0:3] – ‘Ama’
• word[-7:-3] – ‘Amaz’
• word[:7] – ‘Amazing’
• word[0:] – ‘Amazing’
• word[:5] – ‘Amazi’
• word[3:] – ‘zing’
• word[5:] – ‘ng’

• word[:3] + word[3:] – ‘Amazing’


• word[:-7] + word[-7:] – ‘Amazing’

• word[1:6:2] – it will take every 2nd character from index 1 to 5


- ‘mzn’
word[-7:-3:3] – ‘Az’
• Easy way to reverse a string – stringname[ : : -1]
• Eg: word[: : -1] – ‘gnizamA’
• word[: : -2] – ‘giaA’

• word[7] results in error – index out of bounds


• word[0:10] will not result in any error, instead it will consider only the
valid indices.
• word[7:10] – no error, but returns an empty string.
• Q. write a program to access two string of length > 4, and return a
third string, which is made with the first 3 characters of string1 added
to last three characters of string2.

• Q write a program to access a string and check if it is palindrome or


not.

You might also like