Python Programming - Console Input/Output

Exercise : Console Input/Output - General Questions
  • Console Input/Output - General Questions
66.
How can you read a string from the console, replace all spaces with underscores, and print the result?
print(replace_spaces(input("Enter a string: ")))
string_input = input("Enter a string: "); print(string_input.replace(" ", "_"))
replace_chars(get_user_string())
print(replace_whitespace(get_input()))
Answer: Option
Explanation:
To read a string from the console, replace all spaces with underscores, and print the result, use string_input = input("Enter a string: "); print(string_input.replace(" ", "_")).

67.
How can you check if a given year is a leap year or not?
is_leap(int(input("Enter a year: ")))
check_leap_year(get_year())
year = input("Enter a year: "); print(is_leap_year(year))
leap = leap_year(input("Enter a year: "))
Answer: Option
Explanation:
To check if a given year is a leap year or not, use is_leap(int(input("Enter a year: "))).

68.
How can you read a sentence from the console, find the length of each word, and print the result?
word_lengths(input("Enter a sentence: "))
print_lengths(get_sentence())
sentence = input("Enter a sentence: "); print([len(word) for word in sentence.split()])
lengths = find_word_lengths(get_input_sentence())
Answer: Option
Explanation:
To read a sentence from the console, find the length of each word, and print the result, use sentence = input("Enter a sentence: "); print([len(word) for word in sentence.split()]).

69.
How can you prompt the user to enter three numbers and find the maximum among them?
max_num(get_three_numbers())
maximum = find_max(input("Enter three numbers separated by spaces: ")); print(maximum)
max_value = input("Enter three numbers: "); print(find_maximum(max_value))
print("Maximum:", max(input("Enter three numbers: ").split()))
Answer: Option
Explanation:
To prompt the user to enter three numbers and find the maximum among them, use print("Maximum:", max(input("Enter three numbers: ").split())).

70.
How can you read a list of integers from the console, remove duplicates, and print the result?
remove_duplicates(input("Enter integers separated by spaces: "))
numbers = input("Enter integers separated by spaces: "); print(list(set(map(int, numbers.split()))))
unique_values(get_user_input())
print(remove_duplicates(get_numbers()))
Answer: Option
Explanation:
To read a list of integers from the console, remove duplicates, and print the result, use numbers = input("Enter integers separated by spaces: "); print(list(set(map(int, numbers.split())))).