Interview Questions on Python
21. How to convert int to string?
Use str()
Syntax :-
str(number);
Ex :-
str(23); // returns ‘23’
22. How to find the type of variable?
Use type()
Syntax :- type(i)
Ex :-
i = 123
type(i) //type is 'int'
23. How to reverse the string?
Syntax:- stringname[::-1]
Ex: -
s = "String to reverse."
print s[::-1];
24. How to get substring?
Syntax:- stringname[start_index : end_index]
Ex :-
>>> s = 'Hello, everybody!'
>>> s[0]
'H'
>>> s[:3]
'Hel'
>>> s[2:5]
'llo'
25. How to get character from particular position of string?
Use find()
Syntax :- string1.find(“c”); # here c for character
Ex :-
s="mystring"
s.find("r")
Output : 4
26. How to get index of the string?
Use index()
Syntax :- string1.find(“c”); # here c for character
Ex :-
s="mystring"
s.index("r")
Output : 4
27. How to write if statement?
Syntax :-
if conditional_Statement :
#code
Ex: -
if weight > 50:
print("There is a $25 charge for luggage that heavy.")
28. How to write ternary operator?
if else
Ex: -
>>> age = 15
>>> # Conditions are evaluated from left to right
>>> print('kid' if age < 18 else 'adult')
Kid
29. Is there switch statement in python?
No
30. Is there goto statement in python?
No