Exercise: Data Types

Questions for: Data Types

How do you concatenate two strings?
A:
string1.concat(string2)
B:
string1 + string2
C:
string1.append(string2)
D:
concat(string1, string2)
Answer: B
The '+' operator is used for string concatenation in Python.
What will be the result of the following code snippet?
my_string = "Python"
result = my_string.find("th")
print(result)
A:
2
B:
3
C:
th
D:
True
Answer: A
The find() method returns the index of the first occurrence of the specified substring.
What is the purpose of the bool() function?
A:
Converts a string to a boolean
B:
Checks if a variable is of type boolean
C:
Converts a number to a boolean
D:
Creates a boolean variable with a specified value
Answer: C
The bool() function can be used to convert a number to a boolean. 0 becomes False, and any non-zero value becomes True.
What is the output of the following code snippet?
my_dict = {'a': 1, 'b': 2, 'c': 3}
result = 'b' in my_dict
print(result)
A:
True
B:
False
C:
2
D:
Error
Answer: A
The in keyword checks if the specified key is present in the dictionary.
How do you check if a variable is of type float?
A:
isfloat(variable)
B:
type(variable) == float
C:
variable.is_float()
D:
float(variable)
Answer: B
The type() function is used to check the type of a variable.
Ad Slot (Above Pagination)
Quiz