0% found this document useful (0 votes)
6 views16 pages

Python String Slicing and Indexing Guide

Uploaded by

unknown22.7one
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views16 pages

Python String Slicing and Indexing Guide

Uploaded by

unknown22.7one
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

String in Python

Strings and Basic Syntax


• String in Python is a sequence of characters, typically used to
represent text.
• create strings by using single ('Hello') or double quotes ("Hello").
• Multi-line Strings: Triple quotes ('''Hello''' or """Hello""") for multi-
line strings.
Example
Slicing in String
• You all know the meaning of the word ‘slice’, which means – ‘a part
of’.
• In the same way, in python, the term ‘string slice’ refers to a part of
the string, where string are sliced using a range of indices.
• Python defines ":" as string slicing operator. It returns a substring
from the original string. Its general usage is as follows −
• substr=var[x:y] -> x -> begin index , y -> last index
• The first operand x is the index of the first character of the desired
slice. The second operand y is the index of the character next to the
last in the desired string. So var(x:y] separates characters from xth
position to (y-1)th position from the original string.
Program

Output
Accessing Characters and Slicing
• Indexing: Explain how each character in a string has an index, starting
from 0. Demonstrate how to access individual characters using square
brackets ([]).
• Slicing: Introduce slicing to retrieve substrings, showing how it works
with start:stop and start:stop:step.
If no value is provided:
start = 0
end = len(string)
step = 1
S= “Hello”
Operation Example
Slicing from start S[:4] , S[:10]
Slicing from end S[2:] , S[6:]
Negative Indexing S[-4:-2]
Slicing in string
Practice
text = "PYTHON“
print(text[0:3])
print(text[2:5])
print(text[:4])
print(text[3:])
print(text[::2])
print(text[1::2])
print(text[-1])
print(text[-3:])
print(text[:-2])
print(text[::-1])
print(text[5:2:-1])
Answer
Output:
PYT
THO
PYTH
HON
PTO
YHN
N
HON
PYTH
NOHTYP
NOH
Key Points
• Indexing starts at 0 in Python.
• End index is exclusive (not included).
• Negative indices count from the end.
• If step is negative → slicing goes backward.
• If no value is provided:
• start = 0
• end = len(string)
• step = 1
Write output of following program,
1. 2.
s = "INFORMATION“ s = "DATABASE"
print(s[::2]) print(s[-1])
print(s[1::2]) print(s[-4:])
3. print(s[:-3])
s = "LANGUAGE" 4.
print(s[::-1]) s = "PROGRAMMING"
print(s[-2:-7:-1]) print(s[3:9:2])
print(s[::3])
Practice Program
1. Write a program to input an integer and check if it contains any 0 or not.
2. Write a program to print a string in reverse order without storing in new
variable.
3. Write a program to print the string using for loop.
4. Write a program to print the string using while loop.
5. WAP to input two strings. If string1 is contained in string2, then create a
third string with first four characters of string2, added with word
‘Restore’.
6. Write a program to input a string and print the reverse string.
7. Write a program to input a string and check if it is a palindrome string
using slicing.
8. WAP to Count number of vowels in a string (without using
count or in-built).
9. WAP to Count number of words in a sentence (without
using split).
10. WAP to Print characters at even index positions.

You might also like