STD_XI_CSC_WORKSHEET_-_STRING_MANIPULATION1731734109
STD_XI_CSC_WORKSHEET_-_STRING_MANIPULATION1731734109
STD_XI_CSC_WORKSHEET_-_STRING_MANIPULATION1731734109
GERUGAMBAKKAM
STD XI - WORKSHEET – STRING MANIPULATION
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 python4csip.com'
print(s.find('come'))
s.count('o')
Ans:
3
4
1|Page
6 What will be the output of following program:
s='Hello'
for i in s:
print(i,end='#')
Ans:
H#e#l#l#o#
Ans:
H
A
R
D
I
K
Ans:
Welcome
Welcome
Welcome
Welcome
Welcome
a='hello'
b='virat'
for i in range(len(a)):
print(a[i],b[i])
Ans:
hv
ei
lr
la
ot
Hv
Ei
Lr
La
Ot
Ans:
0
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 = "PYTHON4CSIP.COM"
print(str1[1:4], str1[:5], str1[4:], str1[0:-1], str1[:-1])
Ans:
YTH PYTHO ON4CSIP.COM PYTHON4CSIP.CO PYTHON4CSIP.CO
3|Page
15 What will be the output of following program:
str = "my name is kunfu pandya";
print (str.capitalize())
Ans:
My name is kunfu pandya
Ans:
str1 + str2 = HelloWorld!
str1 * 3 = HelloHelloHello
Ans:
3 letters found
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
4|Page
19 What will be the output of following program:
a = "Mahender Singh Dhoni"
a = a.split()
b = a[0][0]+". "+a[1][0]+". "+a[2]
print (b)
Ans:
M. S. Dhoni
Ans:
Mahender,
Singh,
Dhoni
Ans:
Ans:
Hello Python
H
llo Py
lo Python
Hello PythonHello PythonHello Python
Hello PythonString
5|Page
23 What will be the output of the program?
line = "PYTHON IS EASY TO LEARN"
L = line.split('a')
for i in L:
print(i, end=' ')
Ans:
6|Page
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
#
#
#
#
#
#
#
#
#
#
Ans:
mPytho44csi.co
Ans:
NA###5CV###4
7|Page
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
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=string.replace(' ','-')
print("Modified string:")
print(string)
Ans:
8|Page
string=input("Enter string:")
count=0
for i in string:
count=count+1
print("Length of the string is:")
print(count)
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:
9|Page
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)
Ans:
string=input("Enter string:")
count=0
for i in string:
if(i.islower()):
count=count+1
print("The number of lowercase characters is:")
print(count)
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
count1=0
count2=0
for i in string:
if(i.islower()):
count1=count1+1
elif(i.isupper()):
count2=count2+1
print("The number of lowercase characters is:")
print(count1)
print("The number of uppercase characters is:")
print(count2)
Ans:
string=input("Enter string:")
count1=0
count2=0
for i in string:
if(i.isdigit()):
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
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=string.split(" ")
for i in range(0,len(a)):
if(word==a[i]):
count=count+1
print("Count of the word is:")
print(count)
Ans:
string=input("Enter string:")
sub_str=input("Enter word:")
if(string.find(sub_str)==-1):
print("Substring not found in string!")
else:
print("Substring found in string!")
12 | P a g e
41 Write a Python Program to check First Occurrence of a Character in a String
Ans:
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)
Ans:
ch = 'T'
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