Solved: swap case python

The main problem related to swap case in Python is that it does not handle Unicode characters correctly. When using the str.swapcase() method, it only works on ASCII characters and will not work correctly with Unicode characters. This can lead to unexpected results when trying to swap the case of a string containing non-ASCII characters.


def swap_case(s): 
    return s.swapcase() 
  
# Driver program 
s = "This is a Sample String"
print(swap_case(s))

# Line 1: This is a function definition named ‘swap_case’ which takes in one parameter, ‘s’.
# Line 2: This line returns the result of the string method ‘swapcase()’ which will swap all uppercase letters to lowercase and vice versa.
# Line 5: This is a variable declaration, assigning the string “This is a Sample String” to the variable ‘s’.
# Line 6: This line calls the function ‘swap_case’, passing in the variable ‘s’ as an argument. The output of this function will be printed to the console.

swapcase() function

The swapcase() function in Python is used to convert all uppercase characters to lowercase and all lowercase characters to uppercase in a given string. This function does not modify the original string, instead it returns a new string with the swapped cases. For example, if we have a string “Hello World”, the output of swapcase() will be “hELLO wORLD”.

How do you write a Swapcase function in Python

A Swapcase function in Python is a function that takes a string as an argument and returns the same string with all of its characters swapped between upper and lower case.

To write a Swapcase function in Python, you can use the built-in str.swapcase() method. This method takes a single string argument and returns the same string with all of its characters swapped between upper and lower case.

For example, if you have a string “Hello World”, calling str.swapcase() on it would return “hELLO wORLD”.

Here is an example of how to write a Swapcase function in Python:

def swap_case(string):
return string.swapcase()

print(swap_case(“Hello World”)) # Output: hELLO wORLD

Related posts:

Leave a Comment