Python strings questions

How is string interpolation performed in Python?
String interpolation is the process of evaluating a string literal containing one or more placeholders, yielding a result in which the placeholders are replaced with their corresponding values. There are three common ways to interpolate strings in Python:
format function
f-strings
% operator
How to remove all whitespace from a string?
All whitespace characters from a string can be removed by using replace method replacing whitespace with null string
How to remove duplicate characters from a string?
Starting with Python 3.7, dictionary is insertion-ordered. So, dict.fromkeys(mylist) will remove the duplicate characters from the string preserving the original order of the characters in the string. Creating a list with the dict keys and joining the individual characters into a string using the join function will give the string with duplicates removed.
magic = 'abracadabra'
''.join(list(dict.fromkeys(magic)))
'abrcd'
To remove the duplicate characters from a string and the original order of the string need not be preserved, assigning the string to a set and then converting it back to list and joining the individual characters into a string using the join function will give the string with duplicates removed. (sets will not necessarily maintain the order of characters of the string)
magic = 'abracadabra'
''.join(list(set(magic)))
'dracb'
Which of the following code snippets, when executed, would give the output: ['H', 'e', 'l', 'l', 'o']
break('Hello')
split('Hello')
list('Hello')
unpack('Hello')
What would be the output of executing the following code:
len(list('Hi Python!'))
1
9
10
TypeError
What would be the output of executing the following code:
s8 = 'PYTHON'
print(s8[-1])
NOHTYP
P
N
Type Error
What would be the output of executing the following code:
s9 = 'gk' 'nxt'
print(s9)
gknxt
gk nxt
GKNXT
TypeError
What would be the output of executing the following code:
s7 = 'grEat'
s7.upper( )
print(s7)
GREAT
GReAT
great
grEat
What would be the output of executing the following code:
s2 = 'thong'
s4 = 'happy'
print('python' in (s4 + s2))
python
True
False
TypeError
Which of the following code, when executed, does not give Hi as the result?
s = 'Hawai'
print(s[ : :-1][ : :-4])
s = 'Hawai'
print(s[ : :-1] + s[len(s)])
s = 'Hawai'
print(s[ : :-1] [-1]+s[ : :-1][-5])
s = 'Hawai'
print(s[ : :-2] [ : :-2])
What would be the result of executing the following code?
s = 'apple'
print(s.find('p')+s.find('f')+s.find(''))
True
False
0
TypeError
What would be the result of executing the following code?
"USA's $100b-plan q&a on WhatsApp".title( )
"USA's $100b-plan q&a on WhatsApp"
"Usa's $100B-Plan Q&A On Whatsapp"
"USA's $100B-Plan Q&A On WhatsApp"
"Usa'S $100B-Plan Q&A On Whatsapp"
What would be the result of executing the following code?
s = 'the smart owl'
print(s[-9:-100:-2])
aset
set
wo trams
IndexError
Which of the following code, when executed, does not give True as the result?
s = 'Python'
print(s[ : ] == s)
s = 'Python'
print(s[ : :-1] [ : :-1] == s)
s = 'Python'
print(s[ : ] is s)
s = 'Python'
print(s[ : :-1] [ : :-1] is s)
What would be the result of executing the following code?
s = '-4' '\\-2'
print(len(s))
5
6
7
SyntaxError
Which of the following code, when executed, does not give True as the result?
s = 'apple'
print(s.upper( ).lower( ) == s)
s = 'apple'
print(s.strip('-') == s)
s = 'apple'
print('-'.join(s.split('-')) == s)
s = 'apple'
print('-'.join(s.partition('-')) == s)
1. In Python, if you executed name = 'Lizz', what would be the output of print(name[0:2])?
A) Lizz
B) L
C) Li

Answer: Li

Explanation: We only retrieve the first two elements.
2. In Python, if you executed var = '01234567', what would be the result of print(var[::2]).
A) 0246
B) 1357
C) 1234567

Answer: 0246

Explanation: A stride value of 2 selects even elements.
3. In Python, what is the result of the following operation: '1'+'2'
A) 3
B) '3'
C) '12'

Answer: '12'

Explanation: The '+' applied to strings does not add strings but concatenates them
4. What is the result of the following: 'hello'.upper()
A) 'HELLO'
B) 'Hello'
C) 'hello'

Answer: 'HELLO'

Explanation: The .upper returns a copy of the string in which all case-based characters have been converted to uppercase.
5. Consider the string Name="ABCDE", what is the result of the following operation Name.find("B")
A) 1
B) 2
C) 0

Answer: 1

Explanation: The method finds the starting index of a substring
6. What is the result of the following : str(1)+str(1)
A) '11'
B) 2
C) 2

Answer: '11'

Explanation: The integers are cast to a string, and the strings are concatenated
7. What is the result of the following: "123".replace("12", "ab")
A) 'ab3'
B) '123ab'
C) 123

Answer: 'ab3'

Explanation: The method replace returns a copy of the string with all occurrences of the old substring
8. A series of characters between quotes is called what?
A) Data type
B) A string
C) A variable

Answer: A string
9. What code would turn the integer 1 into a string?
A) str(1)
B) int("1")
C) float("1")

Answer: str(1)
10. Code used to get the length of samp_str?
A) length(samp_str)
B) len(samp_str)
C) size(samp_str)

Answer: len(samp_str)
11. Get the 1st character in samp_str?
A) samp_str[1]
B) get(samp_str, 1)
C) samp_str[0]

Answer: samp_str[0]
12. Get the 1st 4 characters in samp_str?
A) samp_str[0:4]
B) samp_str[1:4]
C) samp_str[4]

Answer: samp_str[0:4]
13. Get the last character in samp_str?
A) samp_str[last]
B) samp_str[-1]
C) samp_str[0]

Answer: samp_str[-1]
14. Code used to turn 4 into a string?
A) str(4)
B) string(4)
C) str("4")

Answer: str(4)
15. Get the unicode for A?
A) char("A")
B) unicode("A")
C) ord("A")

Answer: ord("A")
16. Convert the unicode 65 into a character string?
A) chr(65)
B) char(65)
C) str(65)

Answer: chr(65)
gk