0% found this document useful (0 votes)
49 views

Worksheet - String

Uploaded by

cs lab
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Worksheet - String

Uploaded by

cs lab
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Visit Python4csip.com for more updates Visit Python4csip.

com for more updates

s.count('o')
WORKSHEET – STRING HANDLING
Ans:
1 What will be the output of following code-
str="hello"
3
str[:2] 4

6 What will be the output of following program:


Ans:
s='Hello'
'he' for i in s:
print(i,end='#')
2 What will be the output of following code-
Ans:
str='Hello'
res='' H#e#l#l#o#
for i in range(len(str)):
res=res+str[i] 7 What will be the output of following program:
print(res) for i in 'hardik':
print(i.upper())
Ans:
Ans:
Hello
H
A
3 What will be the output of following code-
R
s='Hi!'
D
s1='Hello' I
K
s2=s[:2]+s1[len(s1)-2:]
print(s2) 8 What will be the output of following program:
s='Hello'
Ans: for i in s:
print('Welcome')
Hilo

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

Hv 17 What will be the output of following program:


Ei count = 0
Lr for letter in 'Hello World':
La if(letter == 'l'):
Ot count += 1
print(count,'letters found')

12 What will be the output of following program: Ans:


3 letters found
print("xyyzxyzxzxyy".count('xyy', 2, 11))

3|Page 4|Page
Visit Python4csip.com for more updates Visit Python4csip.com for more updates

['Welcome', 'to', 'Python4csip.com']


18 What will be the output of following program:
22 What will be the output of following program:
s="python4csip" str ='Hello Python'
n = len(s) print (str)
m='' print (str[0])
for i in range(0, n): print (str[2:8])
if (s[i] >= 'a' and s[i] <= 'm'): print (str[3:])
m = m + s[i].upper() print (str * 3)
elif (s[i] >= 'n' and s[i] <= 'z'): print (str + "String")
m = m + s[i-1]
elif (s[i].isupper()): Ans:
m = m + s[i].lower() Hello Python
else: H
m = m + '#' llo Py
print(m) lo Python
Hello PythonHello PythonHello Python
Hello PythonString
Ans:
ppyHho#CcIi
23 What will be the output of the program?
19 What will be the output of following program: line = "PYTHON IS EASY TO LEARN"
a = "Mahender Singh Dhoni"
L = line.split('a')
a = a.split()
b = a[0][0]+". "+a[1][0]+". "+a[2] for i in L:
print (b)
print(i, end=' ')
Ans:
M. S. Dhoni
Ans:

20 What will be the output of following program: PYTHON IS EASY TO LEARN


s='Mahender, Singh, Dhoni'
24 What will be the output of following program:
s1=s.split()
s='mahender, singh, dhoni'
for i in s1:
s1=s.split()
print(i)
for i in s1:
if(i>'n'):
Ans:
Mahender, print(i.upper())
Singh, else:
Dhoni print(i)
Ans:
mahender,
21 What will be the output of following program:
"Welcome to Python4csip.com".split() SINGH,
dhoni
Ans:

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)

35 Write a Program to Check if a String is a Palindrome or Not

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

count1=0 for i in string:


count2=0 count=count+1
for i in string: new=string[0:2]+string[count-2:count]
if(i.islower()): print("Newly formed string is:")
count1=count1+1 print(new)
elif(i.isupper()): 39 Write a Program to Count the Occurrences of Each Word in a Given String
Sentence
count2=count2+1
print("The number of lowercase characters is:") Ans:
print(count1)
string=input("Enter string:")
print("The number of uppercase characters is:")
word=input("Enter word:")
print(count2)
a=[]
count=0
a=string.split(" ")
37 Write a Program to Calculate the Number of Digits and Letters in a String.
for i in range(0,len(a)):
Ans:
if(word==a[i]):
string=input("Enter string:")
count=count+1
count1=0
print("Count of the word is:")
count2=0
print(count)
for i in string:
if(i.isdigit()): 40 Write a Program to Check if a Substring is Present in a Given String
count1=count1+1
Ans:
count2=count2+1
print("The number of digits is:") string=input("Enter string:")
print(count1) sub_str=input("Enter word:")
print("The number of characters is:") if(string.find(sub_str)==-1):
print(count2) print("Substring not found in string!")
else:
print("Substring found in string!")
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 12 | P a g e
Visit Python4csip.com 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

You might also like