Write a Python function to convert a given string to all uppercase if it contains at least 2 uppercase characters in the first 4 characters


This program is written in the Python programming language and it converts the input string to uppercase if at least 2 of the first 4 characters are uppercase, otherwise it returns the original string. It does this in three steps:

  • The first step is to get the input string from the user using the "input" function. The input string is stored in the variable "str".
  • The program then uses a for loop to iterate over the first 4 characters of the string "str". For each iteration, it checks if the current character is uppercase using the built-in "upper" method. If the character is uppercase, the variable "num_upper" is incremented by 1.
  • After the for loop, the program checks if the value of "num_upper" is greater than or equal to 2. If it is, the program converts the entire string "str" to uppercase using the "upper" method and prints it to the console using the "print" function. If not, the original string "str" is simply printed to the console without modification.

Source Code

str = input("Enter the String :")
num_upper = 0
for letter in str[:4]: 
	if letter.upper() == letter:
		num_upper += 1
if num_upper >= 2:
	print(str.upper())
print(str)

Output

Enter the String :ComPuTeR
COMPUTER
ComPuTeR


Example Programs