PYTHON - String
PYTHON - String
MANIPULATION
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(c
h,'-',en
d='')
The above
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
k
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=
'')
PROGRAM TO INPUT ANY STRING AND
COUNT HOW MANY VOWELS IN IT
STRING OPERATORS
A=“Tom”
B=“Jerry”
C=A+” &
”+B
print(C)
Note: you
cannot add
number and
EXAMPLE
Line=“ go”
print(Line*3, ” Govinda”)
str2>str3 True
str3<str1 True
DETERMINING ORDINAL / UNICODE OF
A SINGLE CHARACTER
Python allows us to find out the ordinal position single character
using ord() function.
We can also find out the character based on the ordinal value
using chr() function
Forward indexing 0 1 2 3 4 5 6
message W E L C O M E
-7 -6 -5 -4 -3 -2 -1 Backward
indexing
STRING SLICING
>>> str1="wonderful" >>> str1[3:3]
>>> str1[0:6] ''
'wonder' >>> str1[3:4]
>>> str1[0:3] 'd'
'won' >>> str1[-5:-2]
>>> str1[3:6] 'erf'
'der' >>> str1[:-2]
>>> str1[-1:-3] 'wonderf'
'' >>> str1[:4] Reverse
>>> str1[-3:-1] 'wond‘ of
'fu' >>> str1[-3:] string
>>> str1[-3:0] 'ful‘
'‘ >>>str1[::-1]
>>>str1[0::2] lufrednow
‘Wnefl’ VINOD KUMAR VERMA, PGT(CS), KV OEF
KANPUR & BHARDWAJ, PGT(CS), KV NO.1
SACHIN
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
PROGRAMS
Program to print the pattern Program to input name and
@ print as (if name is ‘AAMIR’),
@@ output
@@@ A
@@@@ AA
@@@@@ AAM
AAMI
string='#' AAMIR
pattern=''
for i in name=
range(5): input("
pattern+=string Enter
print(pattern) any
name")
for i in range(0,len(name)+1):
print(name[0:i])
STRING FUNCTIONS AND METHODS
Python offers many built-in function for string manipulation.
One
method len() we have already used. Let understand
us methods also. other
To use string manipulation function the syntax
is:
String_Object.functionName()
STRING FUNCTIONS/METHODS
• len(string) : this function returns the num ber of characters in any
string including spaces.
• 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.
STRING FUNCTIONS/METHODS
• lower() : this function is used to convert the entire string in small letter.
• 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.
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.
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.
• 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.
STRING FUNCTION / METHODS
• replace(old, new) : this function is used to replace old text inside the string
with new text.
PROGRAM TO ENTER STRING AND COUNT HOW
MANY UPPERCASE, LOWERCASE, DIGITS, WORDS
PRESENT IN IT.
PROGRAM TO ENTER STRING AND FIND THE NUMBER
OF OCCURANCE OF ANY WORD
JUST A MINUTE…
Find out output of following code
fragment:
s1='try'
s2='agai
n' n1=8
n2=5
print(s1+
s2)
print(s2*n2)
print(s1+n1
)
print(s2*s1)
CONSIDER THE FOLLOWING CODE: WHAT WILL BE THE
OUTPUT IF INPUT IS (i) aabbcc (ii) aaccbb (iii) abcc
string = input("Enter a
string :")
count=3
while
True: string = string[2:]
ifelif string[-1] == 'b':
st
ri string=string[:2]
else:
n
g[ count+=1
0]
break
=
print(string)
='
a'
:
print(count
CONSIDER THE FOLLOWING CODE: WHAT WILL BE THE
OUTPUT IF INPUT IS (i) aabbcc (ii) aaccbb (iii) abcc
string = input("Enter a
string :")
If input is aabbcc, output will be
count=3
bbcc
while 4
True: string = string[2:] If input is aaccbb, output will be
ifelif string[-1] == 'b':
cc
st
ri string=string[:2] 4
else:
n If input is abcc, output will be
g[ count+=1 cc
0] 4
break
=
print(string)
='
a'
:
print(count
WHAT WOULD FOLLOWING EXPRESSION RETURN?
a) “Hello
World”.upper().lower()
b) “Hello
World”.lower().upper()
c) “Hello
World”.find(“Wor”,1,6)
d) “Hello World”.find(„Wor‟)
e) “Hello World”.find(„wor‟)
f) “Hello World”.isalpha()
g) “Hello World”.isalnum()
h) “1234”.isdigit()
i) “123GH”.isdigit()
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
)