SlideShare a Scribd company logo
STRING
MANIPULATION
VINOD KUMARVERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
TRAVERSING STRING
• It means accessing the individual characters of string i.e. from first character to
last character.
• Every character in string is at different index position i.e. from 0 to size-1
• For loop can be used to traverse the string very easily
• For .e.g
name="lovely"
for ch in name:
print(ch,'-',end='')
The above code will print l-o-v-e-l-y-
VINOD KUMARVERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
PROGRAM TO READ STRING AND
PRINT IN REVERSE ORDER
string1 = input("Enter any string ")
print("The Reverse of ", string1 , " is :")
length=len(string1)
for ch in range(-1,(-length-1),-1):
print(string1[ch])
The above code will print
Enter any string: karan
n
a
r
a
k VINOD KUMARVERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
PROGRAM TO INPUT STRING AND
PRINT SHORT FORM
string=input("Enter any string ")
print(string[0],".",end='')
for ch in range(1,len(string)):
if string[ch]==' ':
print(string[ch+1],".",end='')
VINOD KUMARVERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
PROGRAM TO INPUT ANY STRING AND
COUNT HOW MANY VOWELS IN IT
VINOD KUMARVERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
STRING OPERATORS
Two basic operators + and * are allowed
+ is used for concatenation (joining)
* Is used for replication (repetition)
VINOD KUMARVERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
EXAMPLE
A=“Tom”
B=“Jerry”
C=A+” & ”+B
print(C)
Note: you cannot add number and string using +
VINOD KUMARVERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
EXAMPLE
Line=“ go”
print(Line*3, ” Govinda”)
Note: you cannot multiply string and string using *
Only number*number or string*number is allowed
VINOD KUMARVERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
MEMBERSHIP OPERATORS
•Membership operators (in and not in) are used to
check the presence of character(s) in any string.
Example Output
„a‟ in „python‟ False
„a‟ in „java‟ True
„per‟ in „operators‟ True
„men‟ in „membership‟ False
„Man‟ in „manipulation‟ False
„Pre‟ not in „presence‟ True
VINOD KUMARVERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
COMPARISON OPERATORS
• We can apply comparison operators (==, !=,>,<,>=,<=) on string.
Comparison will be character by character.
str1=„program‟
str2=„python‟
str3=„Python‟
Example Output
str1==str2 False
str1!=str2 True
str2==„python‟ True
str2>str3 True
str3<str1 True
Comparison of string
will be based on
ASCII code of the
characters
Characters Ordinal/
ASCII code
A-Z 65-90
a-z 97-122
0-9 48-57
VINOD KUMARVERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
DETERMINING ORDINAL / UNICODE OF
A SINGLE CHARACTER
Python allows us to find out the ordinal position single character
using ord() function.
>>>ord(‘A’) output will be 65
We can also find out the character based on the ordinal value
using chr() function
>>>chr(66) output will be ‘B’
VINOD KUMARVERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
STRING SLICING
As we know slice means „part of‟, so slicing is a process of
extracting part of string. In previous chapters we already
discussed that string characters have their unique index
position from 0 to length-1 and -1 to –length(backward)
0 1 2 3 4 5 6
W E L C O M E
-7 -6 -5 -4 -3 -2 -1
Forward indexing
message
Backward indexing
VINOD KUMARVERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
STRING SLICING
>>> str1="wonderful"
>>> str1[0:6]
'wonder'
>>> str1[0:3]
'won'
>>> str1[3:6]
'der'
>>> str1[-1:-3]
''
>>> str1[-3:-1]
'fu'
>>> str1[-3:0]
'‘
>>>str1[0::2]
‘Wnefl’
>>> str1[3:3]
''
>>> str1[3:4]
'd'
>>> str1[-5:-2]
'erf'
>>> str1[:-2]
'wonderf'
>>> str1[:4]
'wond‘
>>> str1[-3:]
'ful‘
>>>str1[::-1]
lufrednow
Reverse
of string
VINOD KUMARVERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
INTERESTING STRING SLICING
For any index position n: str1[:n]+str1[n:] will give you the
original string
>>>str1=“wonderful”
>>>str1[:n]+str[n:] output will be wonderful
String slicing will never return error even if you pass index
which is not in the string . For e.g.
>>>str1[10] will give error, but
>>>str1[10:15] will not give error but return empty
string
VINOD KUMARVERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
PROGRAMS
Program to print the pattern
@
@@
@@@
@@@@
@@@@@
string='#'
pattern=''
for i in range(5):
pattern+=string
print(pattern)
Program to input name and print
as (if name is ‘AAMIR’), output
A
AA
AAM
AAMI
AAMIR
name=input("Enter any name")
for i in range(0,len(name)+1):
print(name[0:i])
VINOD KUMARVERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
STRING FUNCTIONS AND METHODS
Python offers many built-in function for string manipulation. One
method len() we have already used. Let us understand other
methods also.
To use string manipulation function the syntax is:
String_Object.functionName()
VINOD KUMARVERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
STRING FUNCTIONS/METHODS
• len(string) : this function returns the number of characters in any string
including spaces.
• capitalize() : this function is used to convert the first letter of sentence in
capital letter.
• title() : this function is used to convert first letter of every word in string in
capital letters.
• upper() : this function is used to convert the entire string in capital letter.
VINOD KUMARVERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
STRING FUNCTIONS/METHODS
• lower() : this function is used to convert the entire string in small letter.
• count(substring, [start,[end]]) : this function is used to find the number of
occurrence of substring in our string. We can give optional starting index from
where searching begin and also end index i.e. upto what index in string it will
search and count. It return 0 if substring not found.
• find(substring,[start,[end]]) : this function returns the index position of
substring in the given string. We can also specify start and end index just like
count() to modify search criteria. It return -1 if substring not found/
VINOD KUMARVERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
STRING FUNCTIONS/METHODS
• index(substring) : this function return index position of substring. If
substring not it returns error ‘substring not found’
• isalnum() : this function is used to check where string contain all character
as alphanumeric or not. Its return is either True or False.
• islower(): this function return True if all the characters in string is in small
letters
• isupper() : this function return True if all the characters in string is in capital
letters. Both islower() and isupper() will check the case only for letter, if any
symbol present in string it will be ignored.
VINOD KUMARVERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
STRING FUNCTIONS/METHODS
• isspace() :.it return True if the string contains only space.
• isalpha() : it return True if all the characters in string is alphabet.
• isdigit() : it returns True if all the characters in string is digit.
VINOD KUMARVERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
STRING FUNCTIONS/METHODS
• split() : this function is used to split the string based on delimiter and store
the result in the form of list. Default delimiter is space.
• partition(sep) : this function divides the string in the form of tuples of 3
elements known as head, sep and tail. All the string before sep becomes
head and all the string after sep becomes tail. If sep is not present in the
string then everything will becomes head, sep and tail will be empty.
VINOD KUMARVERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
STRING FUNCTIONS/METHODS
• strip([chars]) : it return a copy of string with leading and trailing whitespaces
removed. If chars is given, it remove characters instead.
• lstrip([chars]) : it return a copy of string with leading whitespaces removed. If
chars is given, it remove characters instead.
• rstrip([chars]) : it return a copy of string with trailing whitespaces removed. If
chars is given, it remove characters instead.
• Note: if chars is given in any strip() function, the chars are checked for all the
possible combination that can be formed with the given chars. For e.g. if NOT
is passed as char then NOT, OTN, TON, TNO, ONT,NT like this every possible
combination will be checked.
VINOD KUMARVERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
STRING FUNCTION / METHODS
• replace(old, new) : this function is used to replace old text inside the string
with new text.
for more updates visit: www.python4csip.com
PROGRAM TO ENTER STRING AND COUNT HOW MANY
UPPERCASE, LOWERCASE, DIGITS, WORDS PRESENT IN IT.
VINOD KUMARVERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
PROGRAM TO ENTER STRING AND FIND THE NUMBER OF
OCCURANCE OF ANY WORD
VINOD KUMARVERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
JUST A MINUTE…
Find out output of following code fragment:
s1='try'
s2='again'
n1=8
n2=5
print(s1+s2)
print(s2*n2)
print(s1+n1)
print(s2*s1)
VINOD KUMARVERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
CONSIDER THE FOLLOWING CODE: WHAT WILL BE THE
OUTPUT IF INPUT IS
string = input("Enter a string :")
count=3
whileTrue:
if string[0]=='a':
string = string[2:]
elif string[-1] == 'b':
string=string[:2]
else:
count+=1
break
print(string)
print(count)
(i) aabbcc (ii) aaccbb (iii) abcc
VINOD KUMARVERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
CONSIDER THE FOLLOWING CODE: WHAT WILL BE THE
OUTPUT IF INPUT IS
string = input("Enter a string :")
count=3
whileTrue:
if string[0]=='a':
string = string[2:]
elif string[-1] == 'b':
string=string[:2]
else:
count+=1
break
print(string)
print(count)
(i) aabbcc (ii) aaccbb (iii) abcc
If input is aabbcc, output will be
bbcc
4
If input is aaccbb, output will be
cc
4
If input is abcc, output will be
cc
4
VINOD KUMARVERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
WHAT WOULD FOLLOWING EXPRESSION RETURN?
a) “HelloWorld”.upper().lower()
b) “HelloWorld”.lower().upper()
c) “HelloWorld”.find(“Wor”,1,6)
d) “HelloWorld”.find(„Wor‟)
e) “HelloWorld”.find(„wor‟)
f) “HelloWorld”.isalpha()
g) “HelloWorld”.isalnum()
h) “1234”.isdigit()
i) “123GH”.isdigit()
VINOD KUMARVERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
OUTPUT?
(a)
s="0123456789"
print(s[3],',',s[0:3],'-',s[2:5])
print(s[:3],',',s[3:],'-',s[3:100])
print(s[20:],',',s[2:1],'-',s[1:1])
(b)
y=str(123)
x="hello"*3
print(x,y)
x = "hello" + "world"
y=len(x)
print(y,x)
VINOD KUMARVERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com

More Related Content

PDF
strings11.pdf
PDF
Strings brief introduction in python.pdf
PPTX
Engineering string(681) concept ppt.pptx
PPTX
STRINGS_IN_PYTHON 9-12 (1).pptx
PPTX
string manipulation in python ppt for grade 11 cbse
PPTX
Python Programming-UNIT-II - Strings.pptx
PDF
Python- strings
PDF
Strings in Python
strings11.pdf
Strings brief introduction in python.pdf
Engineering string(681) concept ppt.pptx
STRINGS_IN_PYTHON 9-12 (1).pptx
string manipulation in python ppt for grade 11 cbse
Python Programming-UNIT-II - Strings.pptx
Python- strings
Strings in Python

Similar to 00012.PYTHON - STRING MANIPULATION SLIDES (20)

PPTX
UNIT 4 python.pptx
PPTX
Chapter 11 Strings and methods [Autosaved].pptx
PPTX
Chapter 14 strings
PDF
Python data handling
PPTX
chapte_6_String_python_bca_2005_computer
PPTX
Strings.pptx
PPTX
Chapter 11 Strings.pptx
PPTX
varthini python .pptx
PDF
Python programming : Strings
PPT
PPS_Unit 4.ppt
PPTX
stringggg.pptxtujd7ttttttttttttttttttttttttttttttttttt
PPTX
Detailed description of Strings in Python
PPTX
PDF
String.pdf
PPTX
String Manipulation in Python
PDF
stringsinpython-181122100212.pdf
PPTX
Strings.pptxbfffffffffffffffffffffffffffffffffffffffffd
PPTX
Python String Revisited.pptx
PDF
Strings in python
PPTX
trisha comp ppt.pptx
UNIT 4 python.pptx
Chapter 11 Strings and methods [Autosaved].pptx
Chapter 14 strings
Python data handling
chapte_6_String_python_bca_2005_computer
Strings.pptx
Chapter 11 Strings.pptx
varthini python .pptx
Python programming : Strings
PPS_Unit 4.ppt
stringggg.pptxtujd7ttttttttttttttttttttttttttttttttttt
Detailed description of Strings in Python
String.pdf
String Manipulation in Python
stringsinpython-181122100212.pdf
Strings.pptxbfffffffffffffffffffffffffffffffffffffffffd
Python String Revisited.pptx
Strings in python
trisha comp ppt.pptx
Ad

Recently uploaded (20)

PPTX
Lesson notes of climatology university.
PPTX
GDM (1) (1).pptx small presentation for students
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
01-Introduction-to-Information-Management.pdf
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PPTX
master seminar digital applications in india
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
RMMM.pdf make it easy to upload and study
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Classroom Observation Tools for Teachers
PPTX
Pharma ospi slides which help in ospi learning
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
Lesson notes of climatology university.
GDM (1) (1).pptx small presentation for students
Chinmaya Tiranga quiz Grand Finale.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
O5-L3 Freight Transport Ops (International) V1.pdf
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
01-Introduction-to-Information-Management.pdf
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
master seminar digital applications in india
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
RMMM.pdf make it easy to upload and study
VCE English Exam - Section C Student Revision Booklet
Classroom Observation Tools for Teachers
Pharma ospi slides which help in ospi learning
2.FourierTransform-ShortQuestionswithAnswers.pdf
Ad

00012.PYTHON - STRING MANIPULATION SLIDES

  • 1. STRING MANIPULATION VINOD KUMARVERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 2. TRAVERSING STRING • It means accessing the individual characters of string i.e. from first character to last character. • Every character in string is at different index position i.e. from 0 to size-1 • For loop can be used to traverse the string very easily • For .e.g name="lovely" for ch in name: print(ch,'-',end='') The above code will print l-o-v-e-l-y- VINOD KUMARVERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 3. PROGRAM TO READ STRING AND PRINT IN REVERSE ORDER string1 = input("Enter any string ") print("The Reverse of ", string1 , " is :") length=len(string1) for ch in range(-1,(-length-1),-1): print(string1[ch]) The above code will print Enter any string: karan n a r a k VINOD KUMARVERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 4. PROGRAM TO INPUT STRING AND PRINT SHORT FORM string=input("Enter any string ") print(string[0],".",end='') for ch in range(1,len(string)): if string[ch]==' ': print(string[ch+1],".",end='') VINOD KUMARVERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 5. PROGRAM TO INPUT ANY STRING AND COUNT HOW MANY VOWELS IN IT VINOD KUMARVERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 6. STRING OPERATORS Two basic operators + and * are allowed + is used for concatenation (joining) * Is used for replication (repetition) VINOD KUMARVERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 7. EXAMPLE A=“Tom” B=“Jerry” C=A+” & ”+B print(C) Note: you cannot add number and string using + VINOD KUMARVERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 8. EXAMPLE Line=“ go” print(Line*3, ” Govinda”) Note: you cannot multiply string and string using * Only number*number or string*number is allowed VINOD KUMARVERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 9. MEMBERSHIP OPERATORS •Membership operators (in and not in) are used to check the presence of character(s) in any string. Example Output „a‟ in „python‟ False „a‟ in „java‟ True „per‟ in „operators‟ True „men‟ in „membership‟ False „Man‟ in „manipulation‟ False „Pre‟ not in „presence‟ True VINOD KUMARVERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 10. COMPARISON OPERATORS • We can apply comparison operators (==, !=,>,<,>=,<=) on string. Comparison will be character by character. str1=„program‟ str2=„python‟ str3=„Python‟ Example Output str1==str2 False str1!=str2 True str2==„python‟ True str2>str3 True str3<str1 True Comparison of string will be based on ASCII code of the characters Characters Ordinal/ ASCII code A-Z 65-90 a-z 97-122 0-9 48-57 VINOD KUMARVERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 11. DETERMINING ORDINAL / UNICODE OF A SINGLE CHARACTER Python allows us to find out the ordinal position single character using ord() function. >>>ord(‘A’) output will be 65 We can also find out the character based on the ordinal value using chr() function >>>chr(66) output will be ‘B’ VINOD KUMARVERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 12. STRING SLICING As we know slice means „part of‟, so slicing is a process of extracting part of string. In previous chapters we already discussed that string characters have their unique index position from 0 to length-1 and -1 to –length(backward) 0 1 2 3 4 5 6 W E L C O M E -7 -6 -5 -4 -3 -2 -1 Forward indexing message Backward indexing VINOD KUMARVERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 13. STRING SLICING >>> str1="wonderful" >>> str1[0:6] 'wonder' >>> str1[0:3] 'won' >>> str1[3:6] 'der' >>> str1[-1:-3] '' >>> str1[-3:-1] 'fu' >>> str1[-3:0] '‘ >>>str1[0::2] ‘Wnefl’ >>> str1[3:3] '' >>> str1[3:4] 'd' >>> str1[-5:-2] 'erf' >>> str1[:-2] 'wonderf' >>> str1[:4] 'wond‘ >>> str1[-3:] 'ful‘ >>>str1[::-1] lufrednow Reverse of string VINOD KUMARVERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 14. INTERESTING STRING SLICING For any index position n: str1[:n]+str1[n:] will give you the original string >>>str1=“wonderful” >>>str1[:n]+str[n:] output will be wonderful String slicing will never return error even if you pass index which is not in the string . For e.g. >>>str1[10] will give error, but >>>str1[10:15] will not give error but return empty string VINOD KUMARVERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 15. PROGRAMS Program to print the pattern @ @@ @@@ @@@@ @@@@@ string='#' pattern='' for i in range(5): pattern+=string print(pattern) Program to input name and print as (if name is ‘AAMIR’), output A AA AAM AAMI AAMIR name=input("Enter any name") for i in range(0,len(name)+1): print(name[0:i]) VINOD KUMARVERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 16. STRING FUNCTIONS AND METHODS Python offers many built-in function for string manipulation. One method len() we have already used. Let us understand other methods also. To use string manipulation function the syntax is: String_Object.functionName() VINOD KUMARVERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 17. STRING FUNCTIONS/METHODS • len(string) : this function returns the number of characters in any string including spaces. • capitalize() : this function is used to convert the first letter of sentence in capital letter. • title() : this function is used to convert first letter of every word in string in capital letters. • upper() : this function is used to convert the entire string in capital letter. VINOD KUMARVERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 18. STRING FUNCTIONS/METHODS • lower() : this function is used to convert the entire string in small letter. • count(substring, [start,[end]]) : this function is used to find the number of occurrence of substring in our string. We can give optional starting index from where searching begin and also end index i.e. upto what index in string it will search and count. It return 0 if substring not found. • find(substring,[start,[end]]) : this function returns the index position of substring in the given string. We can also specify start and end index just like count() to modify search criteria. It return -1 if substring not found/ VINOD KUMARVERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 19. STRING FUNCTIONS/METHODS • index(substring) : this function return index position of substring. If substring not it returns error ‘substring not found’ • isalnum() : this function is used to check where string contain all character as alphanumeric or not. Its return is either True or False. • islower(): this function return True if all the characters in string is in small letters • isupper() : this function return True if all the characters in string is in capital letters. Both islower() and isupper() will check the case only for letter, if any symbol present in string it will be ignored. VINOD KUMARVERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 20. STRING FUNCTIONS/METHODS • isspace() :.it return True if the string contains only space. • isalpha() : it return True if all the characters in string is alphabet. • isdigit() : it returns True if all the characters in string is digit. VINOD KUMARVERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 21. STRING FUNCTIONS/METHODS • split() : this function is used to split the string based on delimiter and store the result in the form of list. Default delimiter is space. • partition(sep) : this function divides the string in the form of tuples of 3 elements known as head, sep and tail. All the string before sep becomes head and all the string after sep becomes tail. If sep is not present in the string then everything will becomes head, sep and tail will be empty. VINOD KUMARVERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 22. STRING FUNCTIONS/METHODS • strip([chars]) : it return a copy of string with leading and trailing whitespaces removed. If chars is given, it remove characters instead. • lstrip([chars]) : it return a copy of string with leading whitespaces removed. If chars is given, it remove characters instead. • rstrip([chars]) : it return a copy of string with trailing whitespaces removed. If chars is given, it remove characters instead. • Note: if chars is given in any strip() function, the chars are checked for all the possible combination that can be formed with the given chars. For e.g. if NOT is passed as char then NOT, OTN, TON, TNO, ONT,NT like this every possible combination will be checked. VINOD KUMARVERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 23. STRING FUNCTION / METHODS • replace(old, new) : this function is used to replace old text inside the string with new text. for more updates visit: www.python4csip.com
  • 24. PROGRAM TO ENTER STRING AND COUNT HOW MANY UPPERCASE, LOWERCASE, DIGITS, WORDS PRESENT IN IT. VINOD KUMARVERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 25. PROGRAM TO ENTER STRING AND FIND THE NUMBER OF OCCURANCE OF ANY WORD VINOD KUMARVERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 26. JUST A MINUTE… Find out output of following code fragment: s1='try' s2='again' n1=8 n2=5 print(s1+s2) print(s2*n2) print(s1+n1) print(s2*s1) VINOD KUMARVERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 27. CONSIDER THE FOLLOWING CODE: WHAT WILL BE THE OUTPUT IF INPUT IS string = input("Enter a string :") count=3 whileTrue: if string[0]=='a': string = string[2:] elif string[-1] == 'b': string=string[:2] else: count+=1 break print(string) print(count) (i) aabbcc (ii) aaccbb (iii) abcc VINOD KUMARVERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 28. CONSIDER THE FOLLOWING CODE: WHAT WILL BE THE OUTPUT IF INPUT IS string = input("Enter a string :") count=3 whileTrue: if string[0]=='a': string = string[2:] elif string[-1] == 'b': string=string[:2] else: count+=1 break print(string) print(count) (i) aabbcc (ii) aaccbb (iii) abcc If input is aabbcc, output will be bbcc 4 If input is aaccbb, output will be cc 4 If input is abcc, output will be cc 4 VINOD KUMARVERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 29. WHAT WOULD FOLLOWING EXPRESSION RETURN? a) “HelloWorld”.upper().lower() b) “HelloWorld”.lower().upper() c) “HelloWorld”.find(“Wor”,1,6) d) “HelloWorld”.find(„Wor‟) e) “HelloWorld”.find(„wor‟) f) “HelloWorld”.isalpha() g) “HelloWorld”.isalnum() h) “1234”.isdigit() i) “123GH”.isdigit() VINOD KUMARVERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com
  • 30. OUTPUT? (a) s="0123456789" print(s[3],',',s[0:3],'-',s[2:5]) print(s[:3],',',s[3:],'-',s[3:100]) print(s[20:],',',s[2:1],'-',s[1:1]) (b) y=str(123) x="hello"*3 print(x,y) x = "hello" + "world" y=len(x) print(y,x) VINOD KUMARVERMA, PGT(CS), KV OEF KANPUR & SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR for more updates visit: www.python4csip.com