Exercise: Python

Questions for: Console Io

How can you print a multiline string?
A:
print("Line 1\nLine 2")
B:
multiline_print("Line 1", "Line 2")
C:
print_multiline("Line 1", "Line 2")
D:
print("Line 1")
print("Line 2")
Answer: A
To print a multiline string in Python, use newline characters (\n) to separate lines within a single print() statement.
How can you read a line of text from the console and split it into words?
A:
read_line().split()
B:
line.split(input())
C:
input().split()
D:
split_line(input())
Answer: C
To read a line of text from the console and split it into words, you can use input().split().
How can you align text to the right in a Python console output?
A:
align_right("text")
B:
print("text", align="right")
C:
print(f"{text:>10}")
D:
right_align("text")
Answer: C
Using the f-string format {text:>10} aligns the text to the right within a 10-character wide space.
How can you read a boolean value from the console?
A:
bool(input())
B:
read_boolean()
C:
input().to_bool()
D:
bool(input().lower() == 'true')
Answer: D
To read a boolean value from the console in Python, you can compare the input (converted to lowercase) with the string 'true'.
How can you print a formatted string with variable values?
A:
print("Value is: " + variable)
B:
print("Value is:", variable)
C:
format_print("Value is: {}", variable)
D:
print("Value is: %s" % variable)
Answer: B
Using a comma in the print() function allows you to print a formatted string with variable values in Python.
Ad Slot (Above Pagination)
Quiz