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

Worksheet - String

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

Worksheet - String

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

ASTER PUBLIC SCHOOL, KP-5, GREATER NOIDA WEST

WORKSHEET -1, STRINGS IN PYTHON


CLASS- XI : SESSION- (2024-25)
COMPUTER SCIENCE (083)

1 What will be the output of following code:


str="hello"
str[:2]
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)

Hello
3 What will be the output of following code:
s='Hi!'
s1='Hello'
s2=s[:2]+s1[len(s1)-2:]
print(s2)
Hilo

4 What will be the output of following code:


'ba'+'na'*2
banana
5 What will be the output of following code:
s='Welcome to python4All.com'
print(s.find('come'))
print(s.count('o'))

3
4

6 What will be the output of following program:


s='Hello'
for i in s:
print(i,end='#')
H#e#l#l#o#
7 What will be the output of following program:
for i in 'hardik':
print(i.upper())
H
A
R
D
I
K
8 What will be the output of following program:
s='Hello'
for i in s:
print('Welcome')

Welcome
Welcome
Welcome
Welcome
Welcome
9 What will be the output of following program:
str='virat'
for i in str:
print(str.upper())
VIRAT
VIRAT
VIRAT
VIRAT
VIRAT
10 What will be the output of following program:
str='virat'
for i in str:
print(str.upper())
VIRAT
VIRAT
VIRAT
VIRAT
VIRAT
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])

Hv
Ei
Lr
La
Ot
12 What will be the output of following program:
print("xyyzxyzxzxyy".count('xyy', 2, 11))
0
13 What will be the output of following program:
print(“hello”+1+2+3)
Error
14 What will be the output of following program:
str1 = "PYTHON2Learn.COM"
print(str1[1:4], str1[:5], str1[4:], str1[0:-1], str1[:-1])
YTH PYTHO ON2Learn.COM PYTHON2Learn.CO PYTHON2Learn.CO
15 What will be the output of following program:
str = "my name is kunfu pandya";
print (str.capitalize())
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)

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')

3 letters found
18 What will be the output of following program:

s="python2learn"
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)

npyHho#LEAar
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)
M. S. Dhoni
20 What will be the output of following program:
s='Mahender, Singh, Dhoni'
s1=s.split()
for i in s1:
print(i)
Mahender,
Singh,
Dhoni
21 What will be the output of following program:
print("Welcome to Python2learn.com".split())
['Welcome', 'to', 'Python2learn.com']

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")

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 = line.partition('A')
for i in L:
print(i, end=' ')
print(“\n”, L)

PYTHON IS E A SY TO LEARN
('PYTHON IS E', 'A', 'SY TO LEARN')
24 What will be the output of following program:
s='mahender, singh, dhoni'
s1=s.split()
for i in s1:
if(i>'n'):
print(i.upper())
else:
print(i)

mahender,
SINGH,
dhoni

25 What will be the output of following program:


my_string = 'PYTHON4Learning'
for i in range(len(my_string)):
my_string= '#'
print(my_string)

#
26 What will be the output of following program:
str="Python2learn.happineSS"
for i in range(len(str)):
if(str[i].isalpha()):
print(str[i-1],end='')
if(str[i].isdigit()):
print(str[i],end='')

SPytho22lear.happineS
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='')

NA###5CV###4
28 What will be the output of following program:
str="LOVE 2 LEARN PYTHON"
for i in range(len(str)):
if(str[i].isdigit()):
print(str[i],end='')
if(str[i]=='N'or str[i]=='Y'):
print('#',end='')

2###
29 Write a Program to Count the Number of Vowels in a String.

30 Write a Program to Take in a String and Replace Every Blank Space with Hyphen.

31 Write a Program to Calculate the Length of a String Without Using a Library Function
32 Write a Program to Calculate the Number of Words and the Number of Characters
Present in a String

33 Write a Program to Take in Two Strings and Display the Larger String without Using
Built-in Functions
34 Write a Program to Count Number of Lowercase Characters in a String

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

36 Write a Program to Calculate the Number of Upper-Case Letters and Lower-Case


Letters in a String

37 Write a Program to Form a New String Made of the First 2 and Last 2characters
From a Given String

38 Write a Program to Count the Occurrences of Each Word in a Given StringSentence

39 Write a Program to Calculate the Number of Digits and Special characters in a String

40 Write a Program to Check if a Substring is Present in a Given String

You might also like