6 String
6 String
6 String
CLASS: XII
TOPIC: STRING
TEACHER: MRS.K.BHUVANALAKSHMI
AIM:
To explain about String in python.
GENERAL OBJECTIVES:
By the end of this lesson , children will learn about the basics of string, Operations, Slicing in
stirng..
TEACHING AIDS:
White board, Marker, Text book.
WARM UP ACTIVITY:
Children are generally asked with What they know about strings in python.
DESCRIPTIVE EXPLANATION/Presentation:
Introduction:
Basics of String:
Strings are immutable in python. It means it is unchangeable. At the same memory address,
the new value cannot be stored.
Each character has its index or can be accessed using its index.
String in python has two-way index for each location. (0, 1, 2, ……. In the forward direction
and -1, -2, -3, in the backward direction.)
Example
0 1 2 3 4 5 6 7
k e n d r i y a
-8 -7 -6 -5 -4 -3 -2 -1
The index of string in forward direction starts from 0 and in backward direction starts from -
The size of string is total number of characters present in the string. (If there are n characters
in the string, then last index in forward direction would be n-1 and last index in backward
direction would be –n.)
The character assignment is not supported in string because strings are immutable.
Example :
str = “kendriya”
Traversing a String:
for ch in str :
print(ch, end= „ „)
Output:
kendriya
String Operators:
String concatenation Operator: The + operator creates a new string by joining the two
operand strings.
Example:
>>>”Hello”+”Python” „HelloPython‟
>>>‟2‟+‟7‟
‟27‟
>>>”Python”+”3.0”
„Python3.0‟
Note: You cannot concate numbers and strings as operands with + operator. Example:
>>>7+‟4‟ # unsupported operand type(s) for +: 'int' and 'str' It is invalid and generates an
error.
String repetition Operator: It is also known as String replication operator. It requires two
types of operands- a string and an integer number.
Example:
>>>”you” * 3 „youyouyou‟
>>>3*”you” „youyouyou‟
Note: You cannot have strings as n=both the operands with * operator. Example:
>>>”you” * “you” # can't multiply sequence by non-int of type 'str' It is invalid and
generates an error.
b. Membership Operators:
in – Returns True if a character or a substring exists in the given string; otherwise False
not in - Returns True if a character or a substring does not exist in the given string; otherwise
False
Example:
False
„0‟ to „9‟ 48 to 57
„A‟ to „Z‟ 65 to 90
Example:
True
>>> 'aBcD'<='abCd'
True
Function Description
Example:
>>> ord('b') 98
Program: Write a program to display ASCII code of a character and vice versa.
var=True
while var:
print(ord(ch))
elif choice==2:
else:
if option=='y' or option=='Y':
var=True
else:
var=False
Syntax:
string-name[start:end]
where start and end are integer indices. It returns a string from the index start to end-1.
0 1 2 3 4 5 6 7 8 9 10 11 12
13
d a t a s t r u c t u r
e
Example:
>>> str[0:14]
'data structure'
>>> str[0:6]
'data s'
>>> str[2:7]
'ta st'
>>> str[-13:-6]
'ata str'
>>> str[-5:-11]
>>> str[0:] # Missing index after colon is considered as 14. (length of string) 'data
structure'
Slice operator with strings may have third index. Which is known as step. It is optional.
Syntax:
string-name[start:end:step]
Example:
>>> str[2:9:2]
't tu'
>>> str[-11:-3:3]
'atc'
Interesting Fact: Index out of bounds causes error with strings but slicing a string outside
the index does not cause an error.
Example:
>>>str[14]
>>> str[14:20] # both indices are outside the bounds ' ' # returns empty
string
>>> str[10:16]
'ture'
Reason: When you use an index, you are accessing a particular character of a string, thus the
index must be valid and out of bounds index causes an error as there is no character to return
from the given index.
But slicing always returns a substring or empty string, which is valid sequence.
Example:
str=”data structure”
s4 = „ „
s5= „comp34%@‟
14
'Data structure'
3. find(sub,start,end) Returns the lowest index in the string where the substring sub is found
within the slice range.Returns -1 if sub is not found.
>>> str.find("ruct",5,13)
>>> str.find("ruct",8,13)
-1
4. isalnum( ) Returns True if the characters in the string are alphabets or numbers. False
otherwise
>>>s1.isalnum( ) True
>>>s2.isalnum( ) True
>>>s3.isalnum( ) True
>>>s4.isalnum( ) False
>>>s5.isalnum( ) False
5.isalpha( ) Returns True if all characters in the string are alphabetic. False otherwise.
>>>s1.isalpha( ) False
>>>s2.isalpha( ) True
>>>s3.isalpha( ) False
>>>s4.isalpha( ) False
>>>s5.isalpha( ) False
6.isdigit( ) Returns True if all the characters in the string are digits. False otherwise.
>>>s1.isdigit( ) False
>>>s2.isdigit( ) False
>>>s3.isdigit( ) True
>>>s4.isdigit( ) False
>>>s5.isdigit( ) False
7.islower( ) Returns True if all the characters in the string are lowercase. False otherwise.
>>> s2.islower()True
8. isupper( ) Returns True if all the characters in the string are uppercase. False otherwise.
>>> "HeLlo".lower()
'hello'
>>> "hello".upper()
'HELLO'
12. lstrip( ) Returns a string after removing the leading characters. (Left side).
if used without any argument, it removes the leading whitespaces.
13. rstrip( )Returns a string after removing the trailing characters. (Right side).
14. split( ) breaks a string into words and creates a list out of it
CLASSWORK
1.Write a program that takes a string with multiple words and then capitalize the first
letter of each word and forms a new string out of it.
Solution:
length=len(s1)
a=0 end=length
while a<length:
if a= =0:
s2=s2+s1[0].upper() a+=1
elif (s1[a]==' 'and s1[a+1]!=''): s2=s2+s1[a] s2=s2+s1[a+1].upper() a+=2
else:
s2=s2+s1[a] a+=1
2.Write a program that reads a string and checks whether it is a palindrome string or
not.
mid=n//2 rev=-1
for i in range(mid):
if str[i]==str[rev]:
i=i+1 rev=rev-1
else:
break
else:
print("String is palindrome")
3.Write a program to convert lowercase alphabet into uppercase and vice versa.
if choice==1:
s1=str.lower() print(s1)
elif choice==2:
s1=str.upper() print(s1)
else:
4.Program to read a string and display it in reverse order- display one character per
line. Do not create a reverse string, just display in reverse order.
string1=input(“enter a string:”)
length=len(string1)
for a in range(-1,(-length-1),-1):
print(string[a])
5. Program that prints the following pattern without using any nested loop
##
###
####
#####
string = „#‟
for a in range(5):
pattern+=string
print(pattern)
Assignment
3. Number of alphabets
4. Number of digits
2. Write a program that reads a string and checks whether it is a palindrome string or
not.