Exercise: Python

Questions for: Console Io

How can you read multiple lines of text from the console until a specific keyword is entered?
A:
read_lines_until_keyword("exit")
B:
while True: line = input(); if line == "exit": break; print(line)
C:
lines = input("Enter text (type 'exit' to stop): ")
D:
read_text_with_keyword("exit")
Answer: B
To read multiple lines of text until a specific keyword is entered, use a while loop with a conditional break.
How can you print the current date and time in a specific format?
A:
print(datetime.now())
B:
print(now.strftime("%Y-%m-%d %H:%M:%S"))
C:
print("Current date and time:", datetime.now())
D:
print(f"Current date and time: {now}")
Answer: B
To print the current date and time in a specific format, use strftime("%Y-%m-%d %H:%M:%S") with the datetime object.
How can you read a string from the console and reverse it?
A:
reverse(input())
B:
str.reverse(input())
C:
input()[::-1]
D:
reverse_string(input())
Answer: C
To read a string from the console and reverse it in Python, use slicing with [::-1].
How can you check if a string contains only alphabetic characters?
A:
str.isalpha()
B:
is_alphabetic(str)
C:
check_alpha(str)
D:
str.only_alpha()
Answer: A
The isalpha() method is used to check if a string contains only alphabetic characters in Python.
How can you prompt the user for input and convert it to an integer in a single line?
A:
input().convert_to_int()
B:
int(prompt("Enter an integer: "))
C:
user_input = input("Enter an integer: "); int(user_input)
D:
int(input("Enter an integer: "))
Answer: D
To prompt the user for input and convert it to an integer in a single line, use int(input("Enter an integer: ")).
Ad Slot (Above Pagination)
Quiz