Worksheet - String
Worksheet - String
s.count('o')
WORKSHEET – STRING HANDLING
Ans:
1 What will be the output of following code-
str="hello"
3
str[:2] 4
Ans:
4 What will be the output of following code-
'ba'+'na'*2 Welcome
Welcome
Ans: Welcome
Welcome
'banana' Welcome
5 What will be the output of following code- 9 What will be the output of following program:
s='Welcome to python4csip.com' str='virat'
print(s.find('come')) for i in str:
1|Page 2|Page
Visit Python4csip.com for more updates Visit Python4csip.com for more updates
print(str.upper())
Ans:
Ans: 0
VIRAT
13 What will be the output of following program:
VIRAT
print(“hello”+1+2+3)
VIRAT
Ans:
VIRAT
error
VIRAT 14 What will be the output of following program:
str1 = "PYTHON4CSIP.COM"
10 What will be the output of following program: print(str1[1:4], str1[:5], str1[4:], str1[0:-1], str1[:-1])
a='hello'
b='virat'
Ans:
for i in range(len(a)): YTH PYTHO ON4CSIP.COM PYTHON4CSIP.CO PYTHON4CSIP.CO
print(a[i],b[i])
15 What will be the output of following program:
Ans: str = "my name is kunfu pandya";
hv print (str.capitalize())
ei
lr Ans:
la My name is kunfu pandya
ot
16 What will be the output of following program:
11 What will be the output of following program: str1 = 'Hello'
a='hello' str2 ='World!'
b='virat' print('str1 + str2 = ', str1 + str2)
for i in range(len(a)): print('str1 * 3 =', str1 * 3)
print(a[i].upper(),b[i])
Ans:
str1 + str2 = HelloWorld!
Ans: str1 * 3 = HelloHelloHello
3|Page 4|Page
Visit Python4csip.com for more updates Visit Python4csip.com for more updates
5|Page 6|Page
Visit Python4csip.com for more updates Visit Python4csip.com for more updates
25 What will be the output of following program: 28 What will be the output of following program:
my_string = 'PYTHON4CSIP' str="PYTHON4CSIP"
for i in range(len(my_string)): for i in range(len(str)):
print (my_string) if(str[i].isdigit()):
my_string = '#' print(str[i],end='')
if(str[i]=='N'or str[i]=='Y'):
Ans: print('#',end='')
PYTHON4CSIP
#
# Ans:
# ##4
#
# 29 Write a Program to Count the Number of Vowels in a String.
#
# Ans:
#
# string=input("Enter string:")
#
vowels=0
26 What will be the output of following program: for i in string:
str="Python4csip.com"
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=
for i in range(len(str)):
='U'):
if(str[i].isalpha()):
vowels=vowels+1
print(str[i-1],end='')
print("Number of vowels are:")
if(str[i].isdigit()):
print(vowels)
print(str[i],end='')
Ans: 30 Write a Program to Take in a String and Replace Every Blank Space with
mPytho44csi.co Hyphen.
Ans:
27 What will be the output of following program:
str="AB145CVD124N"
string=input("Enter string:")
for i in range(len(str)):
string=string.replace(' ','-')
if(str[i].isalpha()):
print("Modified string:")
print(str[i-1],end='')
print(string)
if(str[i].isdigit()):
print('#',end='')
31 Write a Program to Calculate the Length of a String Without Using a Library
Function
Ans:
NA###5CV###4 Ans:
7|Page 8|Page
Visit Python4csip.com for more updates Visit Python4csip.com for more updates
count2=count2+1
if(count1<count2):
string=input("Enter string:")
print("Larger string is:")
count=0
print(string2)
for i in string:
elif(count1==count2):
count=count+1
print("Both strings are equal.")
print("Length of the string is:")
else:
print(count)
print("Larger string is:")
print(string1)
32 Write a Program to Calculate the Number of Words and the Number of
Characters Present in a String
34 Write a Program to Count Number of Lowercase Characters in a String
Ans:
string=input("Enter string:")
char=0
Ans:
word=1
string=input("Enter string:")
for i in string:
count=0
char=char+1
for i in string:
if(i==' '):
if(i.islower()):
word=word+1
count=count+1
print("Number of words in the string:")
print("The number of lowercase characters is:")
print(word)
print(count)
print("Number of characters in the string:")
print(char)
33 Write a Program to Take in Two Strings and Display the Larger String without Ans:
Using Built-in Functions
string=input("Enter string:")
Ans: if(string==string[::-1]):
print("The string is a palindrome")
else:
string1=input("Enter first string:")
print("The string isn't a palindrome")
string2=input("Enter second string:")
count1=0
36 Write a Program to Calculate the Number of Upper Case Letters and Lower Case
count2=0
Letters in a String
for i in string1: Ans:
count1=count1+1
string=input("Enter string:")
for j in string2:
9|Page 10 | P a g e
Visit Python4csip.com for more updates Visit Python4csip.com for more updates
Ans:
string=input("Enter string:")
count=0
11 | P a g e 12 | P a g e
Visit Python4csip.com for more updates
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