Class XI : Python Strings

What is String in Python

A string is a sequence of characters, where each character has a unique position/index. The index of string starts from 0 to length-1 in forward direction and -1,-2,-3 …. Length-1 in backward direction.

How to create a string in Python?

Strings can be created by enclosing characters inside a single quote or double quotes. Even triple quotes can be used in Python but generally used to represent multiline strings and docstrings.

# all of the following are equivalent
Example:
n = ‘Hello’
print(n)
output:
Hello

Example:
n = “Hello”
print(n)
output:
Hello

Example:
n = ”’Hello”’
print(n)
output:
Hello

Example:
# triple quotes string can extend to multiple lines
n = “””Hello, welcome to
the world of Python”””
print(n)
Output:
Hello, welcome to
the world of Python

How to access characters in a string?

We can access individual characters using indexing and a range of characters using slicing. Index starts from 0. Trying to access a character out of index range will raise an IndexError.
The index must be an integer. We can’t use float or other types, this will result into TypeError.
Python allows negative indexing for its sequences.
The index of -1 refers to the last item, -2 to the second last item and so on. We can access a range of items in a string by using the slicing operator (colon).

Example:
n=”computer”

Positive index

0

1

2

3

4

5

6

7

c

o

m

p

u

t

e

r

n=”computer”
print(n)
print(n[0])
print(n[2])
print(n[4])
print(n[5])
print(n[7])

output:

computer
c
m
u
t
r

Example:
n1=”catalyst”

Positive index

0

1

2

3

4

5

6

7

c

a

t

a

l

y

s

t

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

output:

catalyst
c
t
l
y
t

Negative indexing

Example:

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])
print(n[-2])
print(n[-4])
print(n[-5])
print(n[-7])

output:

computer
c
e
u
p
o

Example:
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[-2])
print(n1[-3])
print(n1[-6])
print(n1[-7])

catalyst
c
s
y
t
a

Slicing

The meaning of 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 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

slicing with negative index

Example:
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[-3:])
print(n[-7:-2])
print(n[-6:])
print(n[-6:4])

output:

computer
comp
ter
omput
mputer
mp

How to change or delete a string?

Strings are immutable. This means that elements of a string cannot be changed once it has been assigned. We can simply reassign different strings to the same name.
n=”computer”
print(n)
computer
n[2]=’r’
print(n)
#error: TypeError: ‘str’ object does not support item
assignment
Note: we can assign the string by another value but cannot change any character by using index.
n=”computer”
print(n)
n=’hello’
print(n)
computer
hello

Deletion of characters from a string

We cannot delete or remove characters from a string. But deleting the string entirely is possible using the keyword del.
n=”computer”
print(n)
computer
del n[1]
#Error :TypeError: ‘str’ object doesn’t support item deletion
print(n)

But we can delete the entire string
n=”computer”
print(n)
del n
print(n)
Note:
Error :NameError: name ‘n’ is not defined

Number Of Visitors

0270466