Python Programs | IT Developer
IT Developer

Python Programs



Share with a Friend

Python Programs - String

Swap case of all characters in a string - Python Program

Example 1 :

text = "Hello World" print(text.swapcase())

Output

 
OUTPUT  :
hELLO wORLD

Example 2 :

def swap_case_sentence(sentence): """ Swaps the case of each character in a given sentence. Uppercase characters become lowercase, and lowercase characters become uppercase. Non-alphabetic characters remain unchanged. Args: sentence: The input string (sentence). Returns: A new string with the case of characters swapped. """ return sentence.swapcase() # Test cases sentence1 = "Hello World!" sentence2 = "pYtHoN PrOgRaMmInG" sentence3 = "123 ABC def" print(f"Original sentence 1: '{sentence1}'") print(f"Swapped case sentence 1: '{swap_case_sentence(sentence1)}'\n") print(f"Original sentence 2: '{sentence2}'") print(f"Swapped case sentence 2: '{swap_case_sentence(sentence2)}'\n") print(f"Original sentence 3: '{sentence3}'") print(f"Swapped case sentence 3: '{swap_case_sentence(sentence3)}'\n")

Output

 
OUTPUT  :
Original sentence 1: 'Hello World!'
Swapped case sentence 1: 'hELLO wORLD!'

Original sentence 2: 'pYtHoN PrOgRaMmInG'
Swapped case sentence 2: 'PyThOn pRoGrAmMiNg'

Original sentence 3: '123 ABC def'
Swapped case sentence 3: '123 abc DEF'