Python Programming - Console Input/Output

Exercise : Console Input/Output - General Questions
  • Console Input/Output - General Questions
31.
How can you read a string from the console and reverse it?
reverse(input())
str.reverse(input())
input()[::-1]
reverse_string(input())
Answer: Option
Explanation:
To read a string from the console and reverse it in Python, use slicing with [::-1].

32.
How can you print the current date and time in a specific format?
print(datetime.now())
print(now.strftime("%Y-%m-%d %H:%M:%S"))
print("Current date and time:", datetime.now())
print(f"Current date and time: {now}")
Answer: Option
Explanation:
To print the current date and time in a specific format, use strftime("%Y-%m-%d %H:%M:%S") with the datetime object.

33.
How can you read multiple lines of text from the console until a specific keyword is entered?
read_lines_until_keyword("exit")
while True: line = input(); if line == "exit": break; print(line)
lines = input("Enter text (type 'exit' to stop): ")
read_text_with_keyword("exit")
Answer: Option
Explanation:
To read multiple lines of text until a specific keyword is entered, use a while loop with a conditional break.

34.
How can you print a table of values with aligned columns?
print_table(values)
for value in values: print(f"{value:<10}")
table_print(values)
print(f"{values:<10}")
Answer: Option
Explanation:
To print a table of values with aligned columns, use a loop and f-string formatting with alignment.

35.
How can you read a list of strings from the console until an empty line is entered?
read_strings_until_empty()
while True: line = input(); if not line: break; strings.append(line)
input_lines = input("Enter lines (empty line to stop): ")
read_lines_until_empty("exit")
Answer: Option
Explanation:
To read a list of strings until an empty line is entered, use a while loop with a conditional break.