Python String swapcase()


Python String swapcase() method

Python String swapcase() method transforms lower case alphabets to upper case alphabets, and upper case alphabets to lower case alphabets in the given string. The non-alphabetic characters remain unchanged.

Consider that given string is in x.

x = "HELLO world"

Then the return value of x.swapcase() is

hello WORLD

In this tutorial, you will learn the syntax and usage of String swapcase() method in Python language.

Syntax of swapcase() method

The syntax of String swapcase() method in Python is given below.

str.swapcase()

Parameters

The string swapcase() method takes no parameters.

Return value

The string swapcase() method returns a String value.

Examples

In the following examples, we shall see how to use swapcase method for strings containing alphabets, string containing only upper case or lower case alphabets, and for string containing non-alphabetic characters.

1. Swapping case of characters in given string in Python

In this example, we take a string value in variable x. We have to swap case of characters in this string.

Steps

  1. Given a string in x.
  2. Call swapcase() method on the string x.
x.swapcase()
  1. swapcase() method returns a new string with the content of original string x where the characters are swapped of their case. Assign the returned value to a variable, say x_reult.
x_result = x.swapcase()
  1. Print the case swapped string to output.
print(x_result)

Program

The complete program to swap case for characters in a given string is given below.

Python Program

# Take a string
x = "Hello World"

# Swap the case of characters in string
x_result = x.swapcase()

# Print case swapped string
print(x_result)

Output

hELLO wORLD

2. Swap case for an all upper case string string in Python

In this example, we take a string value with all upper case characters in variable x. We have to swap case for characters in this string.

Follow the same steps that we have mentioned in the previous example.

Python Program

# Take a string
x = "HELLO WORLD"

# Swap the case of characters in string
x_result = x.swapcase()

# Print case swapped string
print(x_result)

Output

hello world

The result is a string, where all the characters are transformed to lower case.

Similarly, if the given string contains all lower case characters, then the resulting string by swapcase() method is a string with all upper case characters.

3. Swap case for characters in a string containing non-alphabets in Python

In this example, we take a string value with some non-alphabetic characters in variable x. We have to swap case for characters in this string.

The non-alphabetic characters remain unchanged in the string returned by String swapcase() method.

Python Program

# Take a string
x = "hello 123@ world"

# Swap the case of characters in string
x_result = x.swapcase()

# Print case swapped string
print(x_result)

Output

HELLO 123@ WORLD

123@ in the above string remain unchanged, because they are non-alphabetical characters.

Summary

In this tutorial of Python String Methods, we learned about String swapcase() method, its syntax, and examples.