Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
330 views

Chapter - 7 Strings in Python

The document discusses strings in Python. It defines strings as consecutive sequences of characters and notes that individual characters in a string can be accessed using subscripts starting from 0. It provides examples of creating, accessing, slicing, and comparing strings. It also discusses string methods and built-in functions like count(), lower(), upper(), etc. and provides programs to count characters in a string and check if a string is a palindrome.

Uploaded by

Neelima Vijayan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
330 views

Chapter - 7 Strings in Python

The document discusses strings in Python. It defines strings as consecutive sequences of characters and notes that individual characters in a string can be accessed using subscripts starting from 0. It provides examples of creating, accessing, slicing, and comparing strings. It also discusses string methods and built-in functions like count(), lower(), upper(), etc. and provides programs to count characters in a string and check if a string is a palindrome.

Uploaded by

Neelima Vijayan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Chapter - 7

Strings in Python

Designed by: Umesh Pun (PGT IP)


APS Yol Cantt
Introduction
In python, consecutive sequence of characters is
known as a string.

An individual character in a string is accessed using a


subscript (index).

The subscript should always be an integer (positive or


negative). A subscript starts from 0.

Designed by: Umesh Pun (PGT IP)


APS Yol Cantt
Example

# Declaring a string in python


>>>myfirst=“Save Earth”
>>>print myfirst
Save Earth

#To access the first character of the string


>>>print myfirst[0]
S

Designed by: Umesh Pun (PGT IP)


APS Yol Cantt
Example

To access the fourth character of the string


>>>print myfirst[3]
E

To access the last character of the string


>>>print myfirst[-1]
>>h

Designed by: Umesh Pun (PGT IP)


APS Yol Cantt
Important points about accessing elements in the strings
using subscripts:

• Subscript 0 or –ve n (where n is length of the string)


displays the first element.
• Subscript 1 or –ve (n-1) displays the second element.
Designed by: Umesh Pun (PGT IP)
APS Yol Cantt
a. Creating/Declaring Strings
A literal/constant value to a string can be assigned using a
single quotes, double quotes or triple quotes.

• Enclosing the string in single quotes

Example1
>>>print (‘A friend in need is a friend indeed’)
A friend in need is a friend indeed

Example2
>>>print(‚This book belongs to Raghav\’s sister‛)
This book belongs to Raghav‟s sister
Note: In example 2, to include the single quote within the string it should be
preceded by a backslash.
Designed by: Umesh Pun (PGT IP)
APS Yol Cantt
a. Creating/initialsing Strings
• Enclosing the string in double quotes

Example
>>>print(‚A room without books is like a body without a soul.‛)
A room without books is like a body without a soul.

• Enclosing the string in triple quote

Example
>>>life = ‛‛‛\‛ Live as if you were to die tomorrow.
Learn as if you were to live forever.\‛
---- Mahatma Gandhi ‚‛‛

>>>print life
Note: Triple quotes are used when the text is multiline.
Designed by: Umesh Pun (PGT IP)
APS Yol Cantt
In the above example, backslash (\) is used as an escape sequence.
An escape sequences is nothing but a special character that has a specific
function. As shown above, backslash (\) is used to escape the double quote.

Designed by: Umesh Pun (PGT IP)


APS Yol Cantt
Strings are immutable
Strings are immutable means that the contents of the string
cannot be changed after it is created.

Example
>>>str='honesty'
>>>str[2]='p'
TypeError: 'str' object does not support item assignment

Python does not allow the programmer to change a


character in a string.

Designed by: Umesh Pun (PGT IP)


APS Yol Cantt
b. Traversing a string
Traversing a string means accessing all the elements of the
string one after the other by using the subscript. A string can
be traversed using: for loop or while loop.

Designed by: Umesh Pun (PGT IP)


APS Yol Cantt
c. String Operations

Designed by: Umesh Pun (PGT IP)


APS Yol Cantt
c. String Operations

Designed by: Umesh Pun (PGT IP)


APS Yol Cantt
c. String Operations

Designed by: Umesh Pun (PGT IP)


APS Yol Cantt
String Slicing

Example Output

>>>A=‟Save Earth‟ av
>>> print A[1:3]
>>>print A[3:] ‘e Earth’
>>>print A[:3] Sav
>>>print A[:] ‚Save Earth‛
>>>print A[-2:] ‚th‛
>>>print A[:-2] ‚Save Ear‛
>>>print A[::2] 'Sv at'
>>>printA[::3] 'Seah'
Designed by: Umesh Pun (PGT IP)
APS Yol Cantt
Example of String Comparison

str1=input("Enter String1")
str2=input("Enter String2")

if (str1 > str2):


print(str1, 'is greater than', str2)
elif (str1 < str2):
print(str1, 'is less than', str2)
elif (str1 == str2):
print(str1, 'is equal to', str2)
Designed by: Umesh Pun (PGT IP)
APS Yol Cantt
d. String methods & built in functions

Designed by: Umesh Pun (PGT IP)


APS Yol Cantt
d. String methods & built in functions

Designed by: Umesh Pun (PGT IP)


APS Yol Cantt
d. String methods & built in functions

Designed by: Umesh Pun (PGT IP)


APS Yol Cantt
d. String methods & built in functions

Designed by: Umesh Pun (PGT IP)


APS Yol Cantt
Programs using string functions and operators

Program to count no of ‘p’ in the string pineapple.

word = 'pineapple'
count = 0
for letter in word:
if (letter=='p'):
count = count + 1
print(count)

Designed by: Umesh Pun (PGT IP)


APS Yol Cantt
Program to check whether the string is a palindrome
or not.

str1=input("Enter the String")


L=len(str1)
p=L-1
str=' '
while(p>=0):
str=str+str1[p]
p=p-1

if(str1==str):
print("String is a palindrome")
else:
print("String is not a palindrome")

Designed by: Umesh Pun (PGT IP)


APS Yol Cantt

You might also like