Write a program that converts strings of all uppercase characters into strings of all lowercase and viceversa
Question
Write a program that converts strings of all uppercase characters into strings of all lowercase and viceversa
Solution
Sure, here is a simple Python program that converts strings of all uppercase characters into strings of all lowercase and vice versa:
def convert_case(s):
new_str = ''
for char in s:
if char.isupper():
new_str += char.lower()
elif char.islower():
new_str += char.upper()
else:
new_str += char
return new_str
# Test the function
print(convert_case('Hello WORLD'))
In this program, we define a function convert_case(s) that takes a string s as input. We initialize an empty string new_str. Then, for each character in the input string, we check if it is uppercase or lowercase. If it is uppercase, we convert it to lowercase and add it to new_str. If it is lowercase, we convert it to uppercase and add it to new_str. If the character is neither uppercase nor lowercase (for example, a space or punctuation), we just add it to new_str as it is. Finally, we return new_str.
The line print(convert_case('Hello WORLD')) is used to test the function. It should print hELLO world.
Similar Questions
How can you convert a lowercase character to uppercase manually in a string?
Write a program that takes a string of lowercase characters as input and prints it in uppercase, reversed.
Write a python program to convert all the letters in the string to uppercasestring = "Hello World"print(string.upper())string = "Hello World"print(string.title())string = "Hello World"print(string.capitalize())string = "Hello World"print(string.Upper())
Write a C program to read a sentence and replace lowercase characters with uppercase and vice versa.
Which method can be used to return a string in upper case letters?
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.