Xi Cs Unit-2 Strings
Xi Cs Unit-2 Strings
Prepared By:
Mukesh Chourasia
PGT (Computer Science)
PM Shri Kendriya Vidyalaya No.2 CPE Itarsi
Strings
Syllabus:
String introduction – Definition, examples, initializing, indexing,
traversing a string using loops.
String operations – concatenation/joining, repetition/
replication, membership and slicing, List comparison
built-in functions/methods: len(), capitalize(), title(), lower(),
upper(), count(), find(), index(), endswith(), startswith(),
isalnum(), isalpha(), isdigit(), islower(), isupper(), isspace(),
lstrip(), rstrip(), strip(), replace(), join(), partition(), split()
String: Introduction
Example:
S=‘India@Bharat’
Password=“abc#123”
mob=‘’’8975845885’’’
Address=“A/45, Preet vihar, New Delhi”
Characteristics of a String
Note: A string can be concatenated only with another string and not with any other
data type
String Operations
Syntax
S[Start:Stop:Step]
Slicing a string creates a sub-string of the original string.
The elements of the slice are from start index upto stop-1
index.
Step value is optional & its default value is 1.
>>>S=“I love my country!”
>>>S
‘I love my country!’
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
I L o v e m y c o u n t r y
-17 -16 -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
Both positive index and negative index can be used in slicing. Negative index starts
with -1 on the right upto –n on the left.
String Slice
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
I L o v e m y c o u n t r y
-17 -16 -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
slice is reverse of
the original string
String Slice
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
I L o v e m y c o u n t r y
-17 -16 -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
title() returns a copy of string with >>>S=‘SEASON - five’ >>>S=‘I love my country’
first character of each word >>>S.title() >>>S.title()
in upper case and all other ‘I Love My Country’
‘Season - Five’
letters in lower case
partition() partitions the string into three >>>S1=“I love my country India !”
parts and returns a tuple. >>>S1.partition(‘my’)
1. portion before the substring
( ‘I love’, ‘my’, ‘country India !’ )
2. substring
3. portion after the substring
Note: partition() needs at least one argument
strip() returns a trimmed version of the >>>S1=‘ Know your country ’
string. Trimming is done only >>>S1.strip()
when cases are matched with any
‘Know your country’
of the characters in the
argument from both sides of the >>>S2=‘make in India’ >>>S2.strip(‘Indi’)
string. >>>S2.strip(‘aim’) ‘make in India’
‘ke in Ind’ >>>S2.strip(‘aMk’)
>>>S2.strip(‘amk’) ‘make in Indi’
‘e in Indi’
String Methods & Built-in Functions
Method Description Example
lstrip() returns a trimmed version of the >>>S1=‘ Know your country ’
string. Trimming is done only >>>S1.lstrip()
when cases are matched with any
‘Know your country ’
of the characters in the
argument from left side of the
>>>S2=‘make in India’ >>>S2.lstrip(‘India’)
string. >>>S2.lstrip(‘aim’) ‘make in India’
‘ke in India’ >>>S2.lstrip(‘am’)
>>>S2.lstrip(‘amk’) ‘ke in India’
‘e in India’
rstrip() returns a trimmed version of the >>>S1=‘ Know your country ’
string. Trimming is done only >>>S1.rstrip()
when cases are matched with any
‘ Know your country’
of the characters in the
argument from right side of the >>>S2=‘make in India’ >>>S2.rstrip(‘India’)
string. >>>S2.rstrip(‘aim’) ‘make in ’
‘make in Ind’ >>>S2.rstrip(‘make’)
>>>S2.rstrip(‘amk’) ‘make in Indi’
‘make in Indi’
String Methods & Built-in Functions
Method Description Example
replace() returns a string with a >>>S1=‘I love my country’
substring replaced with
>>>S1.replace(‘country’,’India’)
another substring
Syntax: ‘I love my India’
S.replace(‘substr1’, ‘substr2’)
>>>S2=‘I study in class 11. I love python!’
>>>S2.replace(‘I’, ‘We’)
‘We study in class 11. We love python!’
startswith() returns True if the string >>>S1=‘Python Programming Language’
starts with the given
>>>S1.startswith(‘P’)
substring
True
>>>S1.startswith(‘Pyt’)
True
>>>S1.startswith(‘python’)
False
endswith() returns True if the string >>>S1=‘Python Programming Language’
ends with the given substring
>>>S1.endswith(‘e’)
True
>>>S1.endswith(‘guage’)
True
>>>S1.endswith(‘Age’)
False
String Methods & Built-in Functions
Method Description Example
join() creates a string by joining a >>>S=‘I love my country’
substring to the end of each
>>>’$’.join(‘S’)
character of a string (except
last character) ‘I$ $l$o$v$e$ $m$y$ $I$n$d$i$a’
Syntax: >>>L=[‘I’, ‘Love’, ‘Python’]
S=substring.join(Seq)
>>>’*’.join(L)
Seq-can be sring, list or tuple
‘I*Love*Python’
>>>T=(‘I’, ‘Love’, ‘Python’)
>>>’#@’.join(T)
‘I#@Love#@Python’
swapcase() swaps uppercase letters to >>>S1=‘All is Well’
lowercase and vice-versa
>>>S1.swapcase()
‘aLL IS wELL’