![](https://simplycoding.in/wp-content/uploads/2020/04/Strings-1.jpg)
Python Problems on Strings
- Categories Python, Python Tokens
Python Problems on Strings:
1)Write a program to find the length of the string “refrigerator” without using len function.
Ans.
a = "refrigerator"
count = 0
for i in a:
count = count+1
print (count)
>>> 12
2)Which of the following is not a Python legal string operation?
- ‘abc’+’abc’
- ‘abc’ * 3
- ‘abc’ + 3
- ‘abc’.lower( )
Ans. ‘abc’ + 3 is not legal string operation
3)What would the following expression return?
- “Hello World”.lower().upper()
- “123FGH”.isdigit()
- ‘Hello World’.find(“Wor”)
- ‘Hello World’.isalpha()
Ans.
- HELLO WORLD
- False
- 6
- False
4)What will be the output produced by the following code fragments?
y=str(123) x="hello"*3 print(x,y)
Ans. hellohellohello 123
5)What will be the output produced by the following code fragments?
x="hello"+"world" y=len(x) print(y,x)
Ans. 10 helloworld
6)What will be the output produced by the following code fragments?
x="hello"+"to python"+"world" for char in x: y=char print(y,':',end='')
Ans. h:e:l:lo: :t:o: :p:y:t:h:o:n: :w:o:r:l:d:
7)What will be the output produced by the following code fragments?
x="hello world" print (x[2:-3],x[-4:-2])
Ans. llo wo or
8)What will be the output ?
print('fun'+'trial'+'ooty'*3)
Ans. funtrialootyootyooty
9)Write the expression to reverse the string using slicing method?
Ans.
str="hello world" print (str[::-1])
>>> dlrow olleh
10) What will be the output ?
st="Hello World" print (st.replace(st[6],"#"))
Ans. Hello #orld
11) What will be the output?
x="hello world" print (x[:2],x[:-2],x[-2:])
Ans. he hello wor ld
You may also like
![Tokens1 Tokens1](https://simplycoding.in/wp-content/uploads/2021/06/Tokens1-300x200.jpg)
Python Tokens
15 June, 2021
![Random Module Random Module](https://simplycoding.in/wp-content/uploads/2020/04/Random-Module-1-300x200.jpg)
Practice short problems on Python Random Module
25 April, 2020
![Python Questions on Tokens Python Questions on Tokens](https://simplycoding.in/wp-content/uploads/2020/04/Python-Questions-on-Tokens-300x200.jpg)
Crack CBSE Python Questions on Tokens
24 April, 2020