Python String swapcase() Method

28-12-22 Ahmed Obaid 1031 0

​The swapcase() function converts all uppercase letters to lowercase and vice versa. That is, it converts text strings written in lowercase letters to uppercase letters. Text strings written in uppercase to lowercase letters

 


How to formulate it like this:


string.swapcase()

Parameter: The swapcase() function takes no argument.

return value: The swapcase() function returns a new string with all uppercase letters converted to lowercase and vice versa


In the following example, we will convert uppercase letters to lowercase letters using the swapcase() function

Example:


string = "LEARN PYTHON WITH AHMED OBAID"
print(string.swapcase())

The output will be:


learn python with ahmed obaid

In the following example, we will also convert lowercase letters to uppercase letters using the swapcase() function

Example:


string = "learn python with ahmed obaid"
print(string.swapcase())

The output will be:


LEARN PYTHON WITH AHMED OBAID

If the string consists of lowercase and uppercase letters, the case of the letters will be transposed, i.e. the lowercase letter will be converted to an uppercase letter and the uppercase letter will be converted to a lowercase letter.


string = "TuToRiAl pYthOn"
print(string.swapcase())

 The output will be:


tUtOrIaL PyTHoN

 If the string is made up of non-alphabetic characters, symbols, or numbers, the swapcase() function will return the original string as is.

 Example:


string = "361$<*%^"
print(string.swapcase())

 The output will be:


361$<*%^

 External sources:

 Built-in functions - official Python documentation

 If you have any questions or concerns, leave them in the comments



Tags


Python Python String string methods python python string swapcase method

Share page