Programming and Problem Solving Through Python Language Theory Question
Strings In Python- Set 2
lstrip() method is used for ?
delete all the trailing chracters
delete all the leading chracters
delete all the leading and trailing chracters
delete upper case characters
Answer : B
Explain : strip() function removes any leading, and trailing whitespaces. Lstrip()- It just remove left side leading chracters. L'string' - a leading string to appear at the beginning of the character or numeric data column.
What does "hello".startswith("he") return ?
True
False
Error
None
Answer : A
Explain : startswith() give 'true' if the string start with given value. Other wise give False if not.
Which method is used to split a string into a list of substrings based on a delimiter ?
spilt()
divide()
break()
separate()
Answer : A
Explain : spilt() function is used to spilt the string into list.
What is the output of print('2'+'3') ?
23
'23'
5
2+3
Answer : A
Explain : In this 2 and 3 treated as string, so operation perfom on these string is concatenation which can join these both and give result 23.
What does the following code print?
x = 'mohan'
for i in range(len(x)):
x[i].upper()
print (x)
mohan
MOHAN
Error
None of these
Answer : A
Explain : x[i].upper does not store any variable so it print default x value.
String can be write in ?
'-'
"-"
'''-'''
ALl of these
Answer : D
Explain : String can be written in single quote, double quote and triple quote.
String is
mmutable
immutable
can be change after declare
None of these
Answer : B
Explain : String are immutable ( unchanged ) which can not be change and declare in quote('').
What is the output of given code - print('work'*3) ?
workworkwork
work
work3
work*3
Answer : A
Explain : In this work is string and on string use replication (*), which return work - 3 times.
What is the output of following code ?
x = 'abcd'
for i in range(len(x)):
i.upper()
print (x)
0 1 2 3
a b c d
error
None of these
Answer : C
Explain : When we run this we see an error, 'int' object has no attribute 'upper' because i value goes to 0 1 2 3 (lenght of string) and string upper function does not convert interger value to upper-case.
Which function is used to convert into string ?
sting.variable
tostr()
str()
sting()
Answer : C
Explain : The str() function takes in any python data type and converts it into a string.
What is the output of following code ?
str="This is string"
print(str.istitle())
True
False
Error
Convert all chracter of string in upper case
Answer : B
Explain : istitle() function only checks the first letter and first letter after space is in capital or not. If capital then True else False.
Which of following is output of given code :
stri="string"
print(len(stri))
stri
string
6
5
Answer : C
Explain : len () function is used to check lenght of string. So, string has 6 character so lenght is 6.
What is the output of following code : stri="string" print(stri[4])
ng
stri
n
g
Answer : C
Explain : In this slicing is use, So stri[4] means the letter at 4 no. starting from 0.
What is the output of following code ? a='hello' a='hi' print(a)
hello
hi
hellohi
hihello
Answer : B
Explain : It print the update value of variable.
What is output ? a='hi\'how\'are you' print(a)-
hi'how'are you
hi\'how\'are you
hi are you
'how'
Answer : A
Explain : It show 'how' in quote.
What is the output of following code ? a='abcde' l=len(a) print(l)
4
5
abcde
Error
Answer : B
Explain : In in the value store is lenght of string a which is 5.
What is the index value of o in string 'python' ?
-2
4
5
Both A and B
Answer : D
Explain : From left to right the index value is 4 and from right index value is -2.
Which of following code : a="Hello World" print(a[::-1])
Hello World
d
dlroW olleH
dlroW Hello
Answer : C
Explain : It just reverse the string.
What is the output of following code ? var = "Hello" print(type(var)) var=10 print(type(var))
str and int
Hello and 10
Word and Number
string
Answer : A
Explain : Python type() is a built-in function that is used to return the type of data stored in the objects or variables in the program.