PYTHON TUTORIAL | PYTHON STRINGS



Slicing

The meaning of the word “slice” is “a part of”. In the same way, in python, the term “string slice” refers to a part of the string, where strings are sliced using a range of indices.

Suppose we have a string named “str”. ,

if we give str[n:m]

where n and m are integers and valid indices. Python will return a part of a string by returning the characters falling between indices n and m. starting from n,n+1,n+2,n+3….m-1.

Example:1

n=”computer”

Positive index

0

1

2

3

4

5

6

7

c

O

m

p

u

t

e

r

-8

-7

-6

-5

-4

-3

-2

-1

Negative index

n=”computer”
print(n)
print(n[0:4])
print(n[1:3])
print(n[4:7])
print(n[2:6])
print(n[3:7])

output:

computer
comp
om
ute
mput
pute

n="computer"
print(n)
print(n[0:4])
print(n[1:3])
print(n[4:7])
print(n[2:6])
print(n[3:7])

Output:

computer
comp
om
ute
mput
pute
>>> 

Example:2

n1=”catalyst”

Positive index

0

1

2

3

4

5

6

7

c

a

t

a

l

y

s

t

-8

-7

-6

-5

-4

-3

-2

-1

Negative index

n1=’catalyst’
print(n1)
print(n1[0:7])
print(n1[2:4])
print(n1[3:4])
print(n1[2:6])
print(n1[4:7])

output:
catalyst
catalys
ta
a
taly
lys

n1='catalyst'
print(n1)
print(n1[0:7])
print(n1[2:4])
print(n1[3:4])
print(n1[2:6])
print(n1[4:7])

Output:

catalyst
catalys
ta
a
taly
lys
>>> 

slicing with negative index

Example:3

n=”computer”
Positive index

0

1

2

3

4

5

6

7

c

a

t

a

l

y

s

t

-8

-7

-6

-5

-4

-3

-2

-1

Negative index

n=”computer”
print(n)
print(n[0:4])
print(n[-3:])
print(n[-7:-2])
print(n[-6:])
print(n[-6:4])

output:

computer
comp
ter
omput
mputer
mp

n="computer"
print(n)
print(n[0:4])
print(n[-3:])
print(n[-7:-2])
print(n[-6:])
print(n[-6:4])

Output:

computer
comp
ter
omput
mputer
mp
>>> 

Example:4

n1=”catalyst”

Positive index
0 1 2 3 4 5 6 7
c a t a l y s t
-8 -7 -6 -5 -4 -3 -2 -1
Negative index

n1=’catalyst’
print(n1)
print(n1[-0:])
print(n1[-5:-4])
print(n1[-7:-4])
print(n1[-6:-2])
print(n1[-7:])

output:

catalyst
catalyst
a
ata
taly
atalyst

n1='catalyst'
print(n1)
print(n1[-0:])
print(n1[-5:-4])
print(n1[-7:-4])
print(n1[-6:-2])
print(n1[-7:])

Output:

catalyst
catalyst
a
ata
taly
atalyst
>>> 

combination of positive and negative index

Example:5

n=”computer”
Positive index

0

1

2

3

4

5

6

7

c

o

m

p

u

t

e

r

-8

-7

-6

-5

-4

-3

-2

-1

Negative index
n=”computer”
print(n)
print(n[0:4])
print(n[4:-2])
print(n[-7:2])
print(n[6:-2])
print(n[5:-2])
print(n[2:-2])

output:

computer
comp
ut
o

t
mput

n="computer"
print(n)
print(n[0:4])
print(n[4:-2])
print(n[-7:2])
print(n[6:-2])
print(n[5:-2])
print(n[2:-2])

Output:

computer
comp
ut
o

t
mput
>>> 

Example:6

n1=”catalyst”

Positive index

0

1

2

3

4

5

6

7

c

a

t

a

l

y

s

t

-8

-7

-6

-5

-4

-3

-2

-1

Negative index

n1=’catalyst’
print(n1)
print(n1[-7:7])
print(n1[-5:6])
print(n1[-3:4])
print(n1[-6:6])
print(n1[-4:7])

output:

catalyst
atalys
aly

taly
lys

n1='catalyst'
print(n1)
print(n1[-7:7])
print(n1[-5:6])
print(n1[-3:4])
print(n1[-6:6])
print(n1[-4:7])

Output:

catalyst
atalys
aly

taly
lys
>>> 

Previous : Python String

Next : Modification of python string

Python Basic Programming Tutorial

Python Introduction     Getting started in Python Programming      Python propgramming fundamentals     Python Operators    Python If Condition     Python for loop    Python range construct      Python While loop    break and continue statements     Different looping techniques     Python List     Python String     Python Functions    Python Inbuilt Functions     Python Recursion     Using Python Library     Python Tuples     Python Dictionary     Python Sets     Python Strings     Python Exception Handling     Python Data File Handling

Tutorials

Technical Questions and Important programs

Interview Questions

C Programming
C++ Programming
Basic Python Tutorial
Advanced Python Tutorial
C Language
C++ Programming
Python programming
C Important Programs
C Interview Questions
C++ Interview Questions
Python Interview Questions
HTML Interview Questions


Visitor Counter

0056689