Worksheet - String
Worksheet - String
Ans:
'he'
Ans:
Hello
Ans:
Hilo
Ans:
'banana'
1|Page
Visit Python4csip.com for more updates
s.count('o')
Ans:
3
4
Ans:
H#e#l#l#o#
Ans:
H
A
R
D
I
K
Ans:
Welcome
Welcome
Welcome
Welcome
Welcome
2|Page
Visit Python4csip.com for more updates
print(str.upper())
Ans:
VIRAT
VIRAT
VIRAT
VIRAT
VIRAT
a='hello'
b='virat'
for i in range(len(a)):
print(a[i],b[i])
Ans:
hv
ei
lr
la
ot
Ans:
Hv
Ei
Lr
La
Ot
3|Page
Visit Python4csip.com for more updates
Ans:
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
Ans:
My name is kunfu pandya
Ans:
str1 + str2 = HelloWorld!
str1 * 3 = HelloHelloHello
Ans:
3 letters found
4|Page
Visit Python4csip.com for more updates
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
Ans:
M. S. Dhoni
Ans:
Mahender,
Singh,
Dhoni
Ans:
5|Page
Visit Python4csip.com for more updates
Ans:
Hello Python
H
llo Py
lo Python
Hello PythonHello PythonHello Python
Hello PythonString
Ans:
6|Page
Visit Python4csip.com for more updates
Ans:
PYTHON4CSIP
#
#
#
#
#
#
#
#
#
#
Ans:
mPytho44csi.co
Ans:
NA###5CV###4
7|Page
Visit Python4csip.com for more updates
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
Visit Python4csip.com for more updates
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
Visit Python4csip.com 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)
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
Visit Python4csip.com for more updates
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
Visit Python4csip.com 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=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
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