Exercise: Console Io

Questions for: Console Io

How can you print the ASCII values of all characters in a string?
A:
for char in string: print(ord(char))
B:
ascii_print(string)
C:
print(ascii_values(string))
D:
print(ascii(string))
Answer: A
To print the ASCII values of all characters in a string, use a loop to iterate through each character and print its ASCII value using ord().
How can you check if a string is a valid email address?
A:
check_email(str)
B:
str.isemail()
C:
validate_email(str)
D:
re.match(r'^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$', str)
Answer: D
To check if a string is a valid email address, you can use a regular expression pattern with re.match().
How can you remove all occurrences of a specific character from a string?
A:
remove_char(string, 'x')
B:
string = string.replace('x', '')
C:
string.remove('x')
D:
remove_occurrences(string, 'x')
Answer: B
To remove all occurrences of a specific character from a string, use the replace() method.
How can you read a list of strings from the console until an empty line is entered?
A:
read_strings_until_empty()
B:
while True: line = input(); if not line: break; strings.append(line)
C:
input_lines = input("Enter lines (empty line to stop): ")
D:
read_lines_until_empty("exit")
Answer: B
To read a list of strings until an empty line is entered, use a while loop with a conditional break.
How can you print a table of values with aligned columns?
A:
print_table(values)
B:
for value in values: print(f"{value:<10}")
C:
table_print(values)
D:
print(f"{values:<10}")
Answer: B
To print a table of values with aligned columns, use a loop and f-string formatting with alignment.
Ad Slot (Above Pagination)
Quiz