Visit Python4csip.
com for more updates
WORKSHEET – STRING HANDLING
1 What will be the output of following code-
str="hello"
str[:2]
Ans:
'he'
2 What will be the output of following code-
str='Hello'
res=''
for i in range(len(str)):
res=res+str[i]
print(res)
Ans:
Hello
3 What will be the output of following code-
s='Hi!'
s1='Hello'
s2=s[:2]+s1[len(s1)-2:]
print(s2)
Ans:
Hilo
4 What will be the output of following code-
'ba'+'na'*2
Ans:
'banana'
5 What will be the output of following code-
s='Welcome to [Link]'
print([Link]('come'))
1|Page
Visit [Link] for more updates
[Link]('o')
Ans:
3
4
6 What will be the output of following program:
s='Hello'
for i in s:
print(i,end='#')
Ans:
H#e#l#l#o#
7 What will be the output of following program:
for i in 'hardik':
print([Link]())
Ans:
H
A
R
D
I
K
8 What will be the output of following program:
s='Hello'
for i in s:
print('Welcome')
Ans:
Welcome
Welcome
Welcome
Welcome
Welcome
9 What will be the output of following program:
str='virat'
for i in str:
2|Page
Visit [Link] for more updates
print([Link]())
Ans:
VIRAT
VIRAT
VIRAT
VIRAT
VIRAT
10 What will be the output of following program:
a='hello'
b='virat'
for i in range(len(a)):
print(a[i],b[i])
Ans:
hv
ei
lr
la
ot
11 What will be the output of following program:
a='hello'
b='virat'
for i in range(len(a)):
print(a[i].upper(),b[i])
Ans:
Hv
Ei
Lr
La
Ot
12 What will be the output of following program:
print("xyyzxyzxzxyy".count('xyy', 2, 11))
3|Page
Visit [Link] for more updates
Ans:
13 What will be the output of following program:
print(“hello”+1+2+3)
Ans:
error
14 What will be the output of following program:
str1 = "[Link]"
print(str1[1:4], str1[:5], str1[4:], str1[0:-1], str1[:-1])
Ans:
YTH PYTHO [Link] [Link] [Link]
15 What will be the output of following program:
str = "my name is kunfu pandya";
print ([Link]())
Ans:
My name is kunfu pandya
16 What will be the output of following program:
str1 = 'Hello'
str2 ='World!'
print('str1 + str2 = ', str1 + str2)
print('str1 * 3 =', str1 * 3)
Ans:
str1 + str2 = HelloWorld!
str1 * 3 = HelloHelloHello
17 What will be the output of following program:
count = 0
for letter in 'Hello World':
if(letter == 'l'):
count += 1
print(count,'letters found')
Ans:
3 letters found
4|Page
Visit [Link] for more updates
18 What will be the output of following program:
s="python4csip"
n = len(s)
m=''
for i in range(0, n):
if (s[i] >= 'a' and s[i] <= 'm'):
m = m + s[i].upper()
elif (s[i] >= 'n' and s[i] <= 'z'):
m = m + s[i-1]
elif (s[i].isupper()):
m = m + s[i].lower()
else:
m = m + '#'
print(m)
Ans:
ppyHho#CcIi
19 What will be the output of following program:
a = "Mahender Singh Dhoni"
a = [Link]()
b = a[0][0]+". "+a[1][0]+". "+a[2]
print (b)
Ans:
M. S. Dhoni
20 What will be the output of following program:
s='Mahender, Singh, Dhoni'
s1=[Link]()
for i in s1:
print(i)
Ans:
Mahender,
Singh,
Dhoni
21 What will be the output of following program:
"Welcome to [Link]".split()
Ans:
5|Page
Visit [Link] for more updates
['Welcome', 'to', '[Link]']
22 What will be the output of following program:
str ='Hello Python'
print (str)
print (str[0])
print (str[2:8])
print (str[3:])
print (str * 3)
print (str + "String")
Ans:
Hello Python
H
llo Py
lo Python
Hello PythonHello PythonHello Python
Hello PythonString
23 What will be the output of the program?
line = "PYTHON IS EASY TO LEARN"
L = [Link]('a')
for i in L:
print(i, end=' ')
Ans:
PYTHON IS EASY TO LEARN
24 What will be the output of following program:
s='mahender, singh, dhoni'
s1=[Link]()
for i in s1:
if(i>'n'):
print([Link]())
else:
print(i)
Ans:
mahender,
SINGH,
dhoni
6|Page
Visit [Link] for more updates
25 What will be the output of following program:
my_string = 'PYTHON4CSIP'
for i in range(len(my_string)):
print (my_string)
my_string = '#'
Ans:
PYTHON4CSIP
#
#
#
#
#
#
#
#
#
#
26 What will be the output of following program:
str="[Link]"
for i in range(len(str)):
if(str[i].isalpha()):
print(str[i-1],end='')
if(str[i].isdigit()):
print(str[i],end='')
Ans:
[Link]
27 What will be the output of following program:
str="AB145CVD124N"
for i in range(len(str)):
if(str[i].isalpha()):
print(str[i-1],end='')
if(str[i].isdigit()):
print('#',end='')
Ans:
NA###5CV###4
7|Page
Visit [Link] for more updates
28 What will be the output of following program:
str="PYTHON4CSIP"
for i in range(len(str)):
if(str[i].isdigit()):
print(str[i],end='')
if(str[i]=='N'or str[i]=='Y'):
print('#',end='')
Ans:
##4
29 Write a Program to Count the Number of Vowels in a String.
Ans:
string=input("Enter string:")
vowels=0
for i in string:
if(i=='a' or i=='e' or i=='i' or i=='o' or i=='u' or i=='A' or i=='E' or i=='I' or i=='O' or i=
='U'):
vowels=vowels+1
print("Number of vowels are:")
print(vowels)
30 Write a Program to Take in a String and Replace Every Blank Space with
Hyphen.
Ans:
string=input("Enter string:")
string=[Link](' ','-')
print("Modified string:")
print(string)
31 Write a Program to Calculate the Length of a String Without Using a Library
Function
Ans:
8|Page
Visit [Link] for more updates
string=input("Enter string:")
count=0
for i in string:
count=count+1
print("Length of the string is:")
print(count)
32 Write a Program to Calculate the Number of Words and the Number of
Characters Present in a String
Ans:
string=input("Enter string:")
char=0
word=1
for i in string:
char=char+1
if(i==' '):
word=word+1
print("Number of words in the string:")
print(word)
print("Number of characters in the string:")
print(char)
33 Write a Program to Take in Two Strings and Display the Larger String without
Using Built-in Functions
Ans:
string1=input("Enter first string:")
string2=input("Enter second string:")
count1=0
count2=0
for i in string1:
count1=count1+1
for j in string2:
9|Page
Visit [Link] for more updates
count2=count2+1
if(count1<count2):
print("Larger string is:")
print(string2)
elif(count1==count2):
print("Both strings are equal.")
else:
print("Larger string is:")
print(string1)
34 Write a Program to Count Number of Lowercase Characters in a String
Ans:
string=input("Enter string:")
count=0
for i in string:
if([Link]()):
count=count+1
print("The number of lowercase characters is:")
print(count)
35 Write a Program to Check if a String is a Palindrome or Not
Ans:
string=input("Enter string:")
if(string==string[::-1]):
print("The string is a palindrome")
else:
print("The string isn't a palindrome")
36 Write a Program to Calculate the Number of Upper Case Letters and Lower Case
Letters in a String
Ans:
string=input("Enter string:")
10 | P a g e
Visit [Link] for more updates
count1=0
count2=0
for i in string:
if([Link]()):
count1=count1+1
elif([Link]()):
count2=count2+1
print("The number of lowercase characters is:")
print(count1)
print("The number of uppercase characters is:")
print(count2)
37 Write a Program to Calculate the Number of Digits and Letters in a String.
Ans:
string=input("Enter string:")
count1=0
count2=0
for i in string:
if([Link]()):
count1=count1+1
count2=count2+1
print("The number of digits is:")
print(count1)
print("The number of characters is:")
print(count2)
38 Write a Program to Form a New String Made of the First 2 and Last 2
characters From a Given String
Ans:
string=input("Enter string:")
count=0
11 | P a g e
Visit [Link] for more updates
for i in string:
count=count+1
new=string[0:2]+string[count-2:count]
print("Newly formed string is:")
print(new)
39 Write a Program to Count the Occurrences of Each Word in a Given String
Sentence
Ans:
string=input("Enter string:")
word=input("Enter word:")
a=[]
count=0
a=[Link](" ")
for i in range(0,len(a)):
if(word==a[i]):
count=count+1
print("Count of the word is:")
print(count)
40 Write a Program to Check if a Substring is Present in a Given String
Ans:
string=input("Enter string:")
sub_str=input("Enter word:")
if([Link](sub_str)==-1):
print("Substring not found in string!")
else:
print("Substring found in string!")
12 | P a g e
Visit [Link] for more updates
41 Write a Python Program to check First Occurrence of a Character in a String
Ans:
string = input("Please enter your own String : ")
char = input("Please enter your own Character : ")
flag = 0
for i in range(len(string)):
if(string[i] == char):
flag = 1
break
if(flag == 0):
print("Sorry! We haven't found the Search Character in this string ")
else:
print("The first Occurrence of ", char, " is Found at Position " , i + 1)
42 Write a Python Program to find ASCII Value of a Character
Ans:
ch = 'T'
print("The ASCII Value of %c = %d" %(ch, ord(ch)))
43 Write a Python Program to Remove Last Occurrence of a Character in a String
Ans:
string = input("Please enter your own String : ")
char = input("Please enter your own Character : ")
string2 = ''
length = len(string)
for i in range(length):
if(string[i] == char):
string2 = string[0:i] + string[i + 1:length]
print("Original String : ", string)
print("Final String : ", string2)
13 | P a g e